hookburn-slack 0.2.2

A Rust crate for sending messages to Slack via webhooks
Documentation
// crates/hookburn-slack/src/error.rs
use hookburn_core::CoreError;
use thiserror::Error;

/// Errors that can occur when sending messages to Slack webhooks.
///
/// This error type is returned by all Slack client methods and wraps
/// both transport-level errors and Slack API errors.
#[derive(Error, Debug)]
pub enum SlackError {
    /// An underlying HTTP transport error from reqwest.
    ///
    /// This includes network failures, connection timeouts, and TLS errors.
    #[error("HTTP request failed: {0}")]
    Http(#[from] reqwest::Error),

    /// The message was empty.
    ///
    /// Slack requires either `text` or `blocks` to be present.
    #[error("Message must have text content")]
    EmptyMessage,

    /// Slack rate-limited the request.
    ///
    /// The `retry_after_ms` field indicates how many milliseconds to wait
    /// before retrying.
    #[error("Rate limited by Slack: retry after {retry_after_ms}ms")]
    RateLimited {
        /// Milliseconds to wait before retrying.
        retry_after_ms: u64,
    },

    /// Slack returned a non-success status code.
    ///
    /// Contains the HTTP status code and the error message from Slack's response.
    #[error("Slack API error (HTTP {status}): {message}")]
    ApiError {
        /// HTTP status code returned by Slack.
        status: u16,
        /// Error message from Slack's response body.
        message: String,
    },

    /// JSON serialization failed.
    ///
    /// This typically occurs when the message structure is invalid or contains
    /// unsupported types.
    #[error("JSON serialization failed: {0}")]
    Json(#[from] serde_json::Error),

    /// The provided webhook URL is invalid.
    ///
    /// URLs must use HTTPS and target `hooks.slack.com/services/`.
    #[error("Invalid webhook URL: {reason}")]
    InvalidUrl {
        /// The reason the URL is invalid.
        reason: String,
    },
}

impl From<CoreError> for SlackError {
    fn from(err: CoreError) -> Self {
        match err {
            CoreError::Network(e) => SlackError::Http(e),
            CoreError::Json(e) => SlackError::Json(e),
            CoreError::ServiceNotFound(s) => {
                SlackError::ApiError { status: 404, message: format!("Service not found: {}", s) }
            }
            CoreError::ServiceCreationFailed(s) => SlackError::ApiError {
                status: 500,
                message: format!("Service creation failed: {}", s),
            },
            CoreError::ServiceError(s) => SlackError::ApiError { status: 500, message: s },
            CoreError::InvalidServiceConfig(s) => SlackError::InvalidUrl { reason: s },
            CoreError::MessageConversionFailed(s) => SlackError::ApiError {
                status: 400,
                message: format!("Message conversion failed: {}", s),
            },
            CoreError::RateLimitExceeded { retry_after } => {
                SlackError::RateLimited { retry_after_ms: retry_after.as_millis() as u64 }
            }
            CoreError::ServiceReturnedError { service, status_code, message } => {
                SlackError::ApiError {
                    status: status_code.unwrap_or(500),
                    message: format!("{} error: {}", service, message),
                }
            }
            CoreError::RetryExhausted(s) => {
                SlackError::ApiError { status: 500, message: format!("Retry exhausted: {}", s) }
            }
            // Catch any future variants
            _ => SlackError::ApiError {
                status: 500,
                message: format!("Unexpected core error: {}", err),
            },
        }
    }
}