Skip to main content

comfyui_rs/
error.rs

1use thiserror::Error;
2
3/// Errors returned by ComfyUI operations.
4#[derive(Error, Debug)]
5pub enum ComfyError {
6    /// ComfyUI returned a non-success HTTP status.
7    #[error("ComfyUI returned HTTP {status}: {body}")]
8    Http { status: u16, body: String },
9
10    /// The response from ComfyUI was missing expected fields.
11    #[error("{0}")]
12    InvalidResponse(String),
13
14    /// The queued workflow had node-level errors.
15    #[error("Workflow node errors: {0}")]
16    NodeErrors(String),
17
18    /// Timed out waiting for generation to complete.
19    #[error("Generation timed out")]
20    Timeout,
21
22    /// ComfyUI reported an execution error during generation.
23    #[error("Generation failed: {0}")]
24    GenerationFailed(String),
25
26    /// Download exceeded the configured limit.
27    #[error("Download for '{filename}' exceeded {limit_bytes} bytes (received {actual_bytes})")]
28    OutputTooLarge {
29        filename: String,
30        limit_bytes: usize,
31        actual_bytes: usize,
32    },
33
34    /// Network-level request failure with context.
35    #[error("{context}: {source}")]
36    Network {
37        context: String,
38        source: reqwest::Error,
39    },
40
41    /// JSON serialization/deserialization error.
42    #[error("JSON error: {0}")]
43    Json(#[from] serde_json::Error),
44}
45
46impl ComfyError {
47    /// Returns a stable string discriminant for programmatic matching.
48    pub fn kind(&self) -> &'static str {
49        match self {
50            Self::Http { .. } => "http",
51            Self::InvalidResponse(_) => "invalid_response",
52            Self::NodeErrors(_) => "node_errors",
53            Self::Timeout => "timeout",
54            Self::GenerationFailed(_) => "generation_failed",
55            Self::OutputTooLarge { .. } => "output_too_large",
56            Self::Network { .. } => "network",
57            Self::Json(_) => "json",
58        }
59    }
60}
61
62/// Convenience alias.
63pub type Result<T> = std::result::Result<T, ComfyError>;