|
1 | 1 | use std::path::{Path, PathBuf}; |
2 | 2 |
|
3 | | -use crate::ts::version::Version; |
| 3 | +use crate::ts::error::ApiError; |
| 4 | + |
| 5 | +use crate::game::error::GameError; |
| 6 | +use crate::package::error::PackageError; |
| 7 | +use crate::project::error::ProjectError; |
4 | 8 |
|
5 | | -#[allow(clippy::enum_variant_names)] |
6 | 9 | #[derive(Debug, thiserror::Error)] |
7 | 10 | #[repr(u32)] |
8 | 11 | pub enum Error { |
9 | | - #[error("An API error occurred.")] |
10 | | - ApiError { |
11 | | - source: reqwest::Error, |
12 | | - response_body: Option<String>, |
13 | | - } = 1, |
| 12 | + #[error("{0}")] |
| 13 | + Game(#[from] GameError), |
14 | 14 |
|
15 | | - #[error("A game import error occurred.")] |
16 | | - GameImportError(#[from] crate::game::import::Error), |
| 15 | + #[error("{0}")] |
| 16 | + Package(#[from] PackageError), |
17 | 17 |
|
18 | | - #[error("The file at {0} does not exist or is otherwise not accessible.")] |
19 | | - FileNotFound(PathBuf), |
| 18 | + #[error("{0}")] |
| 19 | + Project(#[from] ProjectError), |
| 20 | + |
| 21 | + #[error("{0}")] |
| 22 | + Api(#[from] ApiError), |
| 23 | + |
| 24 | + #[error("{0}")] |
| 25 | + Io(#[from] IoError), |
| 26 | + |
| 27 | + #[error("{0}")] |
| 28 | + JsonParse(#[from] serde_json::Error), |
20 | 29 |
|
21 | | - #[error("The directory at {0} does not exist or is otherwise not accessible.")] |
22 | | - DirectoryNotFound(PathBuf), |
| 30 | + #[error("{0}")] |
| 31 | + TomlDeserialize(#[from] toml::de::Error), |
23 | 32 |
|
24 | | - #[error("A network error occurred while sending an API request.")] |
25 | | - NetworkError(#[from] reqwest::Error), |
| 33 | + #[error("{0}")] |
| 34 | + TomlSerialize(#[from] toml::ser::Error), |
| 35 | +} |
26 | 36 |
|
27 | | - #[error("The path at {0} is actually a file.")] |
28 | | - ProjectDirIsFile(PathBuf), |
| 37 | +#[derive(Debug, thiserror::Error)] |
| 38 | +pub enum IoError { |
| 39 | + #[error("A file IO error occured: {0}.")] |
| 40 | + Native(std::io::Error, Option<PathBuf>), |
29 | 41 |
|
30 | | - #[error("A project configuration already exists at {0}.")] |
31 | | - ProjectAlreadyExists(PathBuf), |
| 42 | + #[error("File not found: {0}.")] |
| 43 | + FileNotFound(PathBuf), |
32 | 44 |
|
33 | | - #[error("A generic IO error occurred: {0}")] |
34 | | - GenericIoError(#[from] std::io::Error), |
| 45 | + #[error("Expected directory at '{0}', got file.")] |
| 46 | + DirectoryIsFile(PathBuf), |
35 | 47 |
|
36 | | - #[error("A file IO error occurred at path {0}: {1}")] |
37 | | - FileIoError(PathBuf, std::io::Error), |
| 48 | + #[error("Directory not found: {0}.")] |
| 49 | + DirNotFound(PathBuf), |
38 | 50 |
|
39 | | - #[error("Invalid version.")] |
40 | | - InvalidVersion(#[from] crate::ts::version::VersionParseError), |
| 51 | + #[error("{0}")] |
| 52 | + DirWalker(walkdir::Error), |
41 | 53 |
|
42 | | - #[error("Failed to read project file. {0}")] |
43 | | - FailedDeserializeProject(#[from] toml::de::Error), |
| 54 | + #[error("Failed to find file '{0}' within the directory '{1}.")] |
| 55 | + FailedFileSearch(String, PathBuf), |
44 | 56 |
|
45 | | - #[error("No project exists at the path {0}.")] |
46 | | - NoProjectFile(PathBuf), |
| 57 | + #[error("Failed to read subkey at '{0}'.")] |
| 58 | + RegistrySubkeyRead(String), |
| 59 | + |
| 60 | + #[error("Failed to read value with name '{0}' at key '{1}'.")] |
| 61 | + RegistryValueRead(String, String), |
47 | 62 |
|
48 | 63 | #[error("Failed modifying zip file: {0}.")] |
49 | 64 | ZipError(#[from] zip::result::ZipError), |
| 65 | +} |
50 | 66 |
|
51 | | - #[error("Project is missing required table '{0}'.")] |
52 | | - MissingTable(&'static str), |
53 | | - |
54 | | - #[error("Missing repository url.")] |
55 | | - MissingRepository, |
56 | | - |
57 | | - #[error("Missing auth token.")] |
58 | | - MissingAuthToken, |
59 | | - |
60 | | - #[error("The game identifier '{0}' does not exist within the ecosystem schema.")] |
61 | | - InvalidGameId(String), |
62 | | - |
63 | | - #[error("An error occurred while parsing JSON: {0}")] |
64 | | - JsonParserError(#[from] serde_json::Error), |
65 | | - |
66 | | - #[error("An error occured while serializing TOML: {0}")] |
67 | | - TomlSerializer(#[from] toml::ser::Error), |
68 | | - |
69 | | - #[error("The installer does not contain a valid manifest.")] |
70 | | - InstallerNoManifest, |
71 | | - |
72 | | - #[error( |
73 | | - "The installer executable for the current OS and architecture combination does not exist." |
74 | | - )] |
75 | | - InstallerNotExecutable, |
76 | | - |
77 | | - #[error( |
78 | | - " |
79 | | - The installer '{package_id}' does not support the current tcli installer protocol. |
80 | | - Expected: {our_version:#?} |
81 | | - Recieved: {given_version:#?} |
82 | | - " |
83 | | - )] |
84 | | - InstallerBadVersion { |
85 | | - package_id: String, |
86 | | - given_version: Version, |
87 | | - our_version: Version, |
88 | | - }, |
89 | | - |
90 | | - #[error( |
91 | | - "The installer '{package_id}' did not respond correctly: |
92 | | - \t{message}" |
93 | | - )] |
94 | | - InstallerBadResponse { package_id: String, message: String }, |
95 | | - |
96 | | - #[error("The installer returned an error:\n\t{message}")] |
97 | | - InstallerError { message: String }, |
98 | | - |
99 | | - #[error( |
100 | | - "The provided game id '{0}' does not exist or has not been imported into this profile." |
101 | | - )] |
102 | | - BadGameId(String), |
103 | | - |
104 | | - #[error("The Steam app with id '{0}' could not be found.")] |
105 | | - SteamAppNotFound(u32), |
| 67 | +impl From<std::io::Error> for Error { |
| 68 | + fn from(value: std::io::Error) -> Self { |
| 69 | + Self::Io(IoError::Native(value, None)) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl From<reqwest::Error> for Error { |
| 74 | + fn from(value: reqwest::Error) -> Self { |
| 75 | + Self::Api(ApiError::BadRequest { source: value, response_body: None }) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl From<zip::result::ZipError> for Error { |
| 80 | + fn from(value: zip::result::ZipError) -> Self { |
| 81 | + Self::Io(IoError::ZipError(value)) |
| 82 | + } |
106 | 83 | } |
107 | 84 |
|
108 | 85 | pub trait IoResultToTcli<R> { |
109 | | - fn map_fs_error(self, path: impl AsRef<Path>) -> Result<R, Error>; |
| 86 | + fn map_fs_error(self, path: impl AsRef<Path>) -> Result<R, IoError>; |
110 | 87 | } |
111 | 88 |
|
112 | 89 | impl<R> IoResultToTcli<R> for Result<R, std::io::Error> { |
113 | | - fn map_fs_error(self, path: impl AsRef<Path>) -> Result<R, Error> { |
114 | | - self.map_err(|e| Error::FileIoError(path.as_ref().into(), e)) |
| 90 | + fn map_fs_error(self, path: impl AsRef<Path>) -> Result<R, IoError> { |
| 91 | + self.map_err(|e| IoError::Native(e, Some(path.as_ref().into()))) |
115 | 92 | } |
116 | 93 | } |
117 | 94 |
|
118 | 95 | pub trait ReqwestToTcli: Sized { |
119 | | - async fn error_for_status_tcli(self) -> Result<Self, Error>; |
| 96 | + async fn error_for_status_tcli(self) -> Result<Self, ApiError>; |
120 | 97 | } |
121 | 98 |
|
122 | 99 | impl ReqwestToTcli for reqwest::Response { |
123 | | - async fn error_for_status_tcli(self) -> Result<Self, Error> { |
| 100 | + async fn error_for_status_tcli(self) -> Result<Self, ApiError> { |
124 | 101 | match self.error_for_status_ref() { |
125 | 102 | Ok(_) => Ok(self), |
126 | | - Err(err) => Err(Error::ApiError { |
| 103 | + Err(err) => Err(ApiError::BadRequest { |
127 | 104 | source: err, |
128 | 105 | response_body: self.text().await.ok(), |
129 | 106 | }), |
130 | 107 | } |
131 | 108 | } |
132 | 109 | } |
133 | | - |
134 | | -impl From<walkdir::Error> for Error { |
135 | | - fn from(value: walkdir::Error) -> Self { |
136 | | - Self::FileIoError( |
137 | | - value.path().unwrap_or(Path::new("")).into(), |
138 | | - value.into_io_error().unwrap(), |
139 | | - ) |
140 | | - } |
141 | | -} |
0 commit comments