Skip to main content

elemental_driver/
error.rs

1use std::{error::Error as StdError, io, path::PathBuf, time::SystemTimeError};
2
3#[derive(Debug, thiserror::Error)]
4#[non_exhaustive]
5pub enum Error {
6    #[error(transparent)]
7    Io(#[from] io::Error),
8
9    #[error(transparent)]
10    Json(#[from] serde_json::Error),
11
12    #[error(transparent)]
13    Http(#[from] reqwest::Error),
14
15    #[error(transparent)]
16    HttpRequest(#[from] elemental_core::http::HttpRequestError),
17
18    #[error("unsupported driver id: '{id}'")]
19    UnsupportedDriver { id: String },
20
21    #[error("driver '{driver}' requires a loader version")]
22    LoaderVersionRequired { driver: String },
23
24    #[error(transparent)]
25    Infra(#[from] elemental_infra::error::Error),
26
27    #[error(transparent)]
28    Shared(#[from] elemental_shared::error::Error),
29
30    #[error(transparent)]
31    Storage(#[from] elemental_core::storage::Error),
32
33    #[error(transparent)]
34    Runtime(#[from] elemental_core::runtime::Error),
35
36    #[error("blocking task failed: {0}")]
37    BlockingTask(#[from] tokio::task::JoinError),
38
39    #[error(transparent)]
40    Launcher(#[from] elemental_core::launcher::Error),
41
42    #[error(transparent)]
43    SystemTime(#[from] SystemTimeError),
44
45    #[error("{context}: {url}")]
46    InvalidUrl {
47        context: String,
48        url: String,
49        #[source]
50        source: Box<dyn StdError + Send + Sync>,
51    },
52
53    #[error("{context}: {base_url} + {path}")]
54    JoinUrl {
55        context: String,
56        base_url: String,
57        path: String,
58        #[source]
59        source: Box<dyn StdError + Send + Sync>,
60    },
61
62    #[error(transparent)]
63    Origin(#[from] elemental_core::url::OriginError),
64
65    #[error("invalid {label} notation: {notation}")]
66    InvalidNotation { label: String, notation: String },
67
68    #[error("maven library '{notation}' has neither downloads nor a repository url")]
69    MavenLibraryMissingSource { notation: String },
70
71    #[error("decode {source_label} maven metadata failed: {url}")]
72    DecodeMavenMetadata {
73        source_label: String,
74        url: String,
75        #[source]
76        source: Box<dyn StdError + Send + Sync>,
77    },
78
79    #[error("read version metadata failed")]
80    ReadVersionMetadata {
81        #[source]
82        source: Box<Error>,
83    },
84
85    #[error("get version name failed for '{}'", path.display())]
86    MissingVersionName { path: PathBuf },
87
88    #[error("version root has no file name: {}", path.display())]
89    MissingVersionRootFileName { path: PathBuf },
90
91    #[error("logging config not found: {}", path.display())]
92    LoggingConfigMissing { path: PathBuf },
93
94    #[error("module path argument is missing its value")]
95    ModulePathValueMissing,
96
97    #[error("resolve module path argument failed")]
98    ResolveModulePath,
99
100    #[error("version jar not found: {}", path.display())]
101    VersionJarMissing { path: PathBuf },
102
103    #[error("local version '{version}' is not prepared: {status}")]
104    VersionNotPrepared { version: String, status: String },
105
106    #[error("parse base minecraftArguments failed")]
107    ParseBaseMinecraftArguments {
108        #[source]
109        source: Box<Error>,
110    },
111
112    #[error("can't find a valid instance named '{name}'")]
113    InstanceNotFound { name: String },
114
115    #[error("{context} has no parent directory: {}", path.display())]
116    MissingParentDirectory { context: String, path: PathBuf },
117
118    #[error("installer coordinate has no file name: {notation}")]
119    InstallerCoordinateFileNameMissing { notation: String },
120
121    #[error("read persisted {family_name} install profile failed: {}", path.display())]
122    ReadPersistedInstallProfile {
123        family_name: String,
124        path: PathBuf,
125        #[source]
126        source: serde_json::Error,
127    },
128
129    #[error("read persisted {family_name} embedded version failed: {}", path.display())]
130    ReadPersistedEmbeddedVersion {
131        family_name: String,
132        path: PathBuf,
133        #[source]
134        source: serde_json::Error,
135    },
136
137    #[error("{family_name} install profile is missing minecraft version")]
138    InstallerProfileMinecraftVersionMissing { family_name: String },
139
140    #[error("{family_name} install profile is missing {loader_name} version identity")]
141    InstallerProfileLoaderVersionMissing {
142        family_name: String,
143        loader_name: String,
144    },
145
146    #[error(
147        "{family_name} installer game version '{actual}' does not match requested game version '{expected}'"
148    )]
149    InstallerGameVersionMismatch {
150        family_name: String,
151        actual: String,
152        expected: String,
153    },
154
155    #[error(
156        "{family_name} installer loader version '{actual}' does not match requested loader version '{expected}'"
157    )]
158    InstallerLoaderVersionMismatch {
159        family_name: String,
160        actual: String,
161        expected: String,
162    },
163
164    #[error("read {family_name} embedded version json failed: {path}")]
165    ReadEmbeddedVersionJson {
166        family_name: String,
167        path: String,
168        #[source]
169        source: Box<Error>,
170    },
171
172    #[error("download {family_name} installer failed")]
173    DownloadInstaller {
174        family_name: String,
175        #[source]
176        source: elemental_infra::error::Error,
177    },
178
179    #[error("read {family_name} install_profile.json from installer failed")]
180    ReadInstallerProfile {
181        family_name: String,
182        #[source]
183        source: Box<Error>,
184    },
185
186    #[error("extract {family_name} embedded maven artifacts failed")]
187    ExtractInstallerMavenArtifacts {
188        family_name: String,
189        #[source]
190        source: Box<Error>,
191    },
192
193    #[error(
194        "{family_name} installer is missing an embedded version json; launchable {family_name} preparation is not available"
195    )]
196    InstallerEmbeddedVersionMissing { family_name: String },
197
198    #[error("decode {family_name} embedded version json failed")]
199    DecodeEmbeddedVersion {
200        family_name: String,
201        #[source]
202        source: serde_json::Error,
203    },
204
205    #[error("local {family_name} instance '{instance}' is not fully prepared: {status}")]
206    InstallerNotPrepared {
207        family_name: String,
208        instance: String,
209        status: String,
210    },
211
212    #[error("get {family_name} instance name failed for '{}'", path.display())]
213    MissingInstanceName { family_name: String, path: PathBuf },
214
215    #[error("download {family_name} install profile libraries failed")]
216    DownloadInstallerProfileLibraries {
217        family_name: String,
218        #[source]
219        source: elemental_infra::error::Error,
220    },
221
222    #[error(
223        "{family_name} client processors completed without producing the expected client artifacts"
224    )]
225    ProcessorOutputsMissing { family_name: String },
226
227    #[error("unknown {family_name} processor variable: {key}")]
228    UnknownProcessorVariable { family_name: String, key: String },
229
230    #[error("run {family_name} processor failed: {processor}")]
231    RunProcessor {
232        family_name: String,
233        processor: String,
234        #[source]
235        source: io::Error,
236    },
237
238    // ProcessorFailure is large; boxing it keeps every Result<T, Error> small.
239    #[error(transparent)]
240    ProcessorFailed(Box<ProcessorFailure>),
241
242    #[error("jar manifest is missing Main-Class: {}", path.display())]
243    JarMainClassMissing { path: PathBuf },
244
245    #[error("resolve current directory failed for {}", path.display())]
246    CurrentDirectory {
247        path: PathBuf,
248        #[source]
249        source: io::Error,
250    },
251
252    #[error("asset hash is too short: '{hash}'")]
253    AssetHashTooShort { hash: String },
254
255    #[error("can't find vanilla version named '{game_version}'")]
256    VanillaVersionNotFound { game_version: String },
257
258    #[error("rewrite {family_name} upstream url failed for '{raw_url}'")]
259    RewriteUpstream {
260        family_name: String,
261        raw_url: String,
262        #[source]
263        source: Box<Error>,
264    },
265
266    #[error("can't find Rift release asset named '{asset_name}'")]
267    RiftReleaseAssetNotFound { asset_name: String },
268
269    #[error("Rift release tag has no loader version: '{tag_name}'")]
270    RiftReleaseTagMissingLoaderVersion { tag_name: String },
271
272    #[error("read embedded Rift profile failed from '{asset_name}'")]
273    ReadRiftProfile {
274        asset_name: String,
275        #[source]
276        source: elemental_infra::error::Error,
277    },
278
279    #[error("decode embedded Rift profile failed")]
280    DecodeRiftProfile {
281        #[source]
282        source: serde_json::Error,
283    },
284
285    #[error(
286        "Rift loader '{loader_version}' targets Minecraft '{actual}' but '{requested}' was requested"
287    )]
288    RiftGameVersionMismatch {
289        loader_version: String,
290        actual: String,
291        requested: String,
292    },
293
294    #[error("trailing escape in argument string")]
295    TrailingArgumentEscape,
296
297    #[error("unterminated quoted argument string with quote {quote}")]
298    UnterminatedArgumentQuote { quote: char },
299
300    #[error(
301        "metadata patch '{second_patch}' requests mainClass '{second_main_class}', conflicting with '{first_patch}' ('{first_main_class}')"
302    )]
303    PatchMainClassConflict {
304        first_patch: String,
305        first_main_class: String,
306        second_patch: String,
307        second_main_class: String,
308    },
309
310    #[error("metadata patch ordering contains a cycle involving: {}", cycle.join(", "))]
311    PatchOrderCycle { cycle: Vec<String> },
312}
313
314impl Error {
315    pub fn is_not_found(&self) -> bool {
316        match self {
317            Self::Io(source) => source.kind() == io::ErrorKind::NotFound,
318            Self::Storage(elemental_core::storage::Error::ResourceNotFound { .. }) => true,
319            Self::Storage(elemental_core::storage::Error::Io { source, .. }) => {
320                source.kind() == io::ErrorKind::NotFound
321            }
322            Self::Storage(_) => false,
323            _ => false,
324        }
325    }
326}
327
328#[derive(Debug, thiserror::Error)]
329#[non_exhaustive]
330#[error(
331    "{family_name} processor failed: processor={processor}, java={java}, main_class={main_class}, classpath={classpath}, args={args:?}, exit_status={exit_status:?}, stdout=\n{stdout}\nstderr=\n{stderr}"
332)]
333pub struct ProcessorFailure {
334    pub family_name: String,
335    pub processor: String,
336    pub java: String,
337    pub main_class: String,
338    pub classpath: String,
339    pub args: Vec<String>,
340    pub exit_status: Option<i32>,
341    pub stdout: String,
342    pub stderr: String,
343}
344
345impl Error {
346    pub(crate) fn invalid_url(
347        context: impl Into<String>,
348        url: impl Into<String>,
349        source: impl StdError + Send + Sync + 'static,
350    ) -> Self {
351        Self::InvalidUrl {
352            context: context.into(),
353            url: url.into(),
354            source: Box::new(source),
355        }
356    }
357
358    pub(crate) fn join_url(
359        context: impl Into<String>,
360        base_url: impl Into<String>,
361        path: impl Into<String>,
362        source: impl StdError + Send + Sync + 'static,
363    ) -> Self {
364        Self::JoinUrl {
365            context: context.into(),
366            base_url: base_url.into(),
367            path: path.into(),
368            source: Box::new(source),
369        }
370    }
371}
372
373pub type Result<T> = std::result::Result<T, Error>;