Skip to main content

slack_web_api/
error.rs

1use crate::ResponseMetadata;
2use std::time::Duration;
3
4/// Error returned by a Web API call.
5#[derive(Debug, thiserror::Error)]
6pub enum SlackError {
7    /// Slack answered `"ok": false` (`invalid_auth`, `channel_not_found`, ...).
8    #[error("slack api error: {}", .0.error)]
9    Api(Box<SlackApiError>),
10    /// Still rate limited (429) after the configured number of retries.
11    #[error("rate limited by slack (retry after {retry_after:?})")]
12    RateLimited { retry_after: Option<Duration> },
13    /// A non-2xx status with a body that is not a Slack JSON response.
14    #[error("unexpected http status {status}")]
15    Http { status: u16, body: String },
16    /// Connection or transfer failure.
17    #[error("transport error: {0}")]
18    Transport(#[from] reqwest::Error),
19    /// The response body could not be decoded into the response type.
20    #[error("failed to decode response: {source}")]
21    Decode {
22        source: serde_json::Error,
23        body: String,
24    },
25    /// The request could not be encoded as a form body.
26    #[error("failed to encode request: {0}")]
27    Encode(String),
28    /// A successful response lacked a field needed for the next step.
29    #[error("response is missing {0}")]
30    MissingField(&'static str),
31    /// A background upload task stopped before finishing.
32    #[error("background task failed: {0}")]
33    Task(String),
34}
35
36/// Details of an `"ok": false` response.
37#[derive(Debug, Clone, PartialEq)]
38pub struct SlackApiError {
39    /// The `error` code, such as `channel_not_found`.
40    pub error: String,
41    /// The `warning` value.
42    pub warning: Option<String>,
43    /// Details. The reasons for `invalid_blocks` and similar errors are in `messages`.
44    pub response_metadata: Option<ResponseMetadata>,
45}
46
47impl SlackError {
48    /// The `error` code returned by Slack, or `None` for non-API errors.
49    pub fn api_error(&self) -> Option<&str> {
50        match self {
51            SlackError::Api(e) => Some(e.error.as_str()),
52            _ => None,
53        }
54    }
55}