mold_core/error.rs
1use thiserror::Error;
2
3/// Primary error type for the mold crate ecosystem.
4///
5/// Provides structured error variants for the main failure modes so callers
6/// can pattern-match on specific categories instead of string-matching.
7/// The `Other` variant accepts any `anyhow::Error` as a catch-all.
8#[derive(Debug, Error)]
9pub enum MoldError {
10 /// Request validation failures (bad dimensions, empty prompt, etc.)
11 #[error("{0}")]
12 Validation(String),
13
14 /// Download/network issues
15 #[error("{0}")]
16 Download(String),
17
18 /// Config parsing/loading errors
19 #[error("{0}")]
20 Config(String),
21
22 /// HTTP client communication errors
23 #[error("{0}")]
24 Client(String),
25
26 /// Model doesn't exist or isn't downloaded
27 #[error("{0}")]
28 ModelNotFound(String),
29
30 /// Inference/generation errors
31 #[error("{0}")]
32 Inference(String),
33
34 /// Generic RunPod API error (non-auth, non-not-found)
35 #[error("{0}")]
36 RunPod(String),
37
38 /// RunPod authentication failure (401/403 or missing/invalid API key)
39 #[error("{0}")]
40 RunPodAuth(String),
41
42 /// RunPod resource not found (404)
43 #[error("{0}")]
44 RunPodNotFound(String),
45
46 /// RunPod could not schedule the pod (no machines available)
47 #[error("{0}")]
48 RunPodNoStock(String),
49
50 /// Catch-all for everything else
51 #[error(transparent)]
52 Other(#[from] anyhow::Error),
53}
54
55/// Convenience alias used across the crate.
56pub type Result<T> = std::result::Result<T, MoldError>;
57
58impl From<crate::download::DownloadError> for MoldError {
59 fn from(err: crate::download::DownloadError) -> Self {
60 MoldError::Download(err.to_string())
61 }
62}