Skip to main content

autumn_plugin_teams/
error.rs

1//! Error types.
2
3use crate::auth::AuthError;
4
5/// Error from a handler. Any error type converts with `?`.
6pub type HandlerError = Box<dyn std::error::Error + Send + Sync + 'static>;
7
8/// Result of a handler.
9pub type HandlerResult<T> = Result<T, HandlerError>;
10
11/// Plugin and client errors. Messages never hold a token or a secret.
12#[derive(Debug, thiserror::Error)]
13#[non_exhaustive]
14pub enum TeamsError {
15    /// Bad config or bad input from the app.
16    #[error("teams config: {0}")]
17    Config(String),
18    /// The Connector replied with an error status.
19    #[error("teams {operation} failed: http {status} {code}")]
20    Api {
21        /// Client operation, for example `send_to_conversation`.
22        operation: &'static str,
23        /// HTTP status.
24        status: u16,
25        /// Error code from the `ErrorResponse`, or `unknown`.
26        code: String,
27        /// Error message from the `ErrorResponse`, if any.
28        message: Option<String>,
29    },
30    /// The Connector replied 429 and the client stopped.
31    #[error("teams rate limited (retry after {retry_after_secs:?} s)")]
32    RateLimited {
33        /// The `Retry-After` value, if any.
34        retry_after_secs: Option<u64>,
35    },
36    /// The token endpoint did not give a token.
37    #[error("teams token request failed: {code}")]
38    Token {
39        /// The OAuth error code, for example `invalid_client`, or an HTTP or
40        /// network code.
41        code: String,
42    },
43    /// No HTTP reply: connect error or timeout.
44    #[error("teams transport: {0}")]
45    Transport(String),
46    /// The reply is not the expected JSON.
47    #[error("teams reply decode: {0}")]
48    Decode(String),
49    /// The `serviceUrl` is not an allowed Connector URL.
50    #[error("serviceUrl is not an allowed Connector URL")]
51    BadServiceUrl,
52    /// The JWT check failed.
53    #[error(transparent)]
54    Auth(#[from] AuthError),
55}
56
57impl TeamsError {
58    /// Returns the Connector error code for an [`TeamsError::Api`] error.
59    #[must_use]
60    pub fn api_code(&self) -> Option<&str> {
61        match self {
62            Self::Api { code, .. } => Some(code),
63            _ => None,
64        }
65    }
66}