Skip to main content

hookburn_core/
error.rs

1// crates/hookburn-core/src/error.rs
2use std::fmt;
3use thiserror::Error;
4
5/// Helper for formatting optional status codes in error messages
6struct StatusCodeDisplay(Option<u16>);
7
8impl fmt::Display for StatusCodeDisplay {
9    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10        match self.0 {
11            Some(code) => write!(f, "{code}"),
12            None => write!(f, "unknown"),
13        }
14    }
15}
16
17/// Most common error types
18#[non_exhaustive]
19#[derive(Debug, Error)]
20pub enum CoreError {
21    /// An HTTP or network-level failure from the underlying reqwest client.
22    #[error("network error: {0}")]
23    Network(#[from] reqwest::Error),
24
25    /// JSON serialization or deserialization failed.
26    #[error("JSON error: {0}")]
27    Json(#[from] serde_json::Error),
28
29    /// Service not found in registry.
30    #[error("service not found: {0}")]
31    ServiceNotFound(String),
32
33    /// Service creation failed.
34    #[error("service creation failed: {0}")]
35    ServiceCreationFailed(String),
36
37    /// Service returned an error.
38    #[error("service error: {0}")]
39    ServiceError(String),
40
41    /// Invalid service configuration.
42    #[error("invalid service configuration: {0}")]
43    InvalidServiceConfig(String),
44
45    /// Message conversion failed.
46    #[error("message conversion failed: {0}")]
47    MessageConversionFailed(String),
48
49    /// Rate limit exceeded.
50    #[error("rate limit exceeded, retry after {retry_after:?}")]
51    RateLimitExceeded {
52        /// Length of time to retry connection
53        retry_after: std::time::Duration,
54    },
55
56    /// Service returned an error with status code.
57    #[error("service {service} returned error {}: {message}", StatusCodeDisplay(*status_code))]
58    ServiceReturnedError {
59        /// Services
60        service: String,
61        /// Code
62        status_code: Option<u16>,
63        /// Message
64        message: String,
65    },
66
67    /// All retry attempts failed.
68    #[error("all retry attempts failed: {0}")]
69    RetryExhausted(String),
70}