Skip to main content

hookburn_slack/
error.rs

1// crates/hookburn-slack/src/error.rs
2use hookburn_core::CoreError;
3use thiserror::Error;
4
5/// Errors that can occur when sending messages to Slack webhooks.
6///
7/// This error type is returned by all Slack client methods and wraps
8/// both transport-level errors and Slack API errors.
9#[derive(Error, Debug)]
10pub enum SlackError {
11    /// An underlying HTTP transport error from reqwest.
12    ///
13    /// This includes network failures, connection timeouts, and TLS errors.
14    #[error("HTTP request failed: {0}")]
15    Http(#[from] reqwest::Error),
16
17    /// The message was empty.
18    ///
19    /// Slack requires either `text` or `blocks` to be present.
20    #[error("Message must have text content")]
21    EmptyMessage,
22
23    /// Slack rate-limited the request.
24    ///
25    /// The `retry_after_ms` field indicates how many milliseconds to wait
26    /// before retrying.
27    #[error("Rate limited by Slack: retry after {retry_after_ms}ms")]
28    RateLimited {
29        /// Milliseconds to wait before retrying.
30        retry_after_ms: u64,
31    },
32
33    /// Slack returned a non-success status code.
34    ///
35    /// Contains the HTTP status code and the error message from Slack's response.
36    #[error("Slack API error (HTTP {status}): {message}")]
37    ApiError {
38        /// HTTP status code returned by Slack.
39        status: u16,
40        /// Error message from Slack's response body.
41        message: String,
42    },
43
44    /// JSON serialization failed.
45    ///
46    /// This typically occurs when the message structure is invalid or contains
47    /// unsupported types.
48    #[error("JSON serialization failed: {0}")]
49    Json(#[from] serde_json::Error),
50
51    /// The provided webhook URL is invalid.
52    ///
53    /// URLs must use HTTPS and target `hooks.slack.com/services/`.
54    #[error("Invalid webhook URL: {reason}")]
55    InvalidUrl {
56        /// The reason the URL is invalid.
57        reason: String,
58    },
59}
60
61impl From<CoreError> for SlackError {
62    fn from(err: CoreError) -> Self {
63        match err {
64            CoreError::Network(e) => SlackError::Http(e),
65            CoreError::Json(e) => SlackError::Json(e),
66            CoreError::ServiceNotFound(s) => {
67                SlackError::ApiError { status: 404, message: format!("Service not found: {}", s) }
68            }
69            CoreError::ServiceCreationFailed(s) => SlackError::ApiError {
70                status: 500,
71                message: format!("Service creation failed: {}", s),
72            },
73            CoreError::ServiceError(s) => SlackError::ApiError { status: 500, message: s },
74            CoreError::InvalidServiceConfig(s) => SlackError::InvalidUrl { reason: s },
75            CoreError::MessageConversionFailed(s) => SlackError::ApiError {
76                status: 400,
77                message: format!("Message conversion failed: {}", s),
78            },
79            CoreError::RateLimitExceeded { retry_after } => {
80                SlackError::RateLimited { retry_after_ms: retry_after.as_millis() as u64 }
81            }
82            CoreError::ServiceReturnedError { service, status_code, message } => {
83                SlackError::ApiError {
84                    status: status_code.unwrap_or(500),
85                    message: format!("{} error: {}", service, message),
86                }
87            }
88            CoreError::RetryExhausted(s) => {
89                SlackError::ApiError { status: 500, message: format!("Retry exhausted: {}", s) }
90            }
91            // Catch any future variants
92            _ => SlackError::ApiError {
93                status: 500,
94                message: format!("Unexpected core error: {}", err),
95            },
96        }
97    }
98}