sfr_types/
error.rs

1//! The type that represents all erros in `slack-framework-rs`.
2
3mod api;
4mod core;
5mod server;
6
7pub use api::ApiError;
8pub use core::CoreError;
9pub use server::ServerError;
10
11/// The `Box<Error>`.
12type BoxError = Box<dyn std::error::Error + 'static + Send + Sync>;
13
14/// The type that represents all erros in `slack-framework-rs`.
15#[derive(thiserror::Error, Debug)]
16pub enum Error {
17    /// The error with Slack API Client.
18    #[error("error in API Client: {0}")]
19    Api(#[from] ApiError),
20
21    /// The error with commonly process.
22    #[error("error in commonly process: {0}")]
23    Core(#[from] CoreError),
24
25    /// The error with server process.
26    #[error("error about server: {0}")]
27    Server(#[from] ServerError),
28
29    /// The error when the required value is missing.
30    #[error("{0}")]
31    RequiredValueIsMissing(String),
32
33    /// The error that occurs with failed to deserialize JSON data.
34    #[error("failed to deserialize: {0}")]
35    DeserializeJson(serde_json::Error),
36
37    /// The stacked error.
38    #[error("stacked error: message = {message}, stack = {stack:?}")]
39    Stacked {
40        /// The error message for this stack.
41        message: &'static str,
42
43        /// The Stacked errors.
44        stack: Option<Box<Error>>,
45    },
46}
47
48impl Error {
49    /// Creates [`Error::RequiredValueIsMissing`].
50    pub fn required_value_is_missing(values: &[&str]) -> Self {
51        if values.is_empty() {
52            Self::RequiredValueIsMissing("Some required value is missing.".into())
53        } else if values.len() == 1 {
54            Self::RequiredValueIsMissing(format!("`{}` is required.", values.first().unwrap()))
55        } else {
56            Self::RequiredValueIsMissing(format!("One of `{}` is required.", values.join("` or `")))
57        }
58    }
59
60    /// Creates the stacked error.
61    pub fn stack(self, message: &'static str) -> Self {
62        Self::Stacked {
63            message,
64            stack: Some(Box::new(self)),
65        }
66    }
67
68    /// Creates [`ApiError::FailedToRequestByHttp`].
69    pub fn failed_to_request_by_http(
70        inner: &'static str,
71        e: impl std::error::Error + 'static + Send + Sync,
72    ) -> Self {
73        Self::Api(ApiError::FailedToRequestByHttp(inner, Box::new(e)))
74    }
75
76    /// Creates [`ApiError::FailedToReadJson`].
77    pub fn failed_to_read_json(
78        inner: &'static str,
79        e: impl std::error::Error + 'static + Send + Sync,
80    ) -> Self {
81        Self::Api(ApiError::FailedToReadJson(inner, Box::new(e)))
82    }
83
84    /// Creates [`ApiError::FailedToReadStream`].
85    pub fn failed_to_read_stream(
86        inner: &'static str,
87        e: impl std::error::Error + 'static + Send + Sync,
88    ) -> Self {
89        Self::Api(ApiError::FailedToReadStream(inner, Box::new(e)))
90    }
91
92    /// Creates [`ApiError::FailedToReadFileInFilesUpload`].
93    pub fn failed_to_read_file_in_files_upload(
94        e: impl std::error::Error + 'static + Send + Sync,
95    ) -> Self {
96        Self::Api(ApiError::FailedToReadFileInFilesUpload(Box::new(e)))
97    }
98
99    /// Creates [`ApiError::FailedCreatingMulipartData`].
100    pub fn failed_creating_mulipart_data(
101        e: impl std::error::Error + 'static + Send + Sync,
102    ) -> Self {
103        Self::Api(ApiError::FailedCreatingMulipartData(Box::new(e)))
104    }
105
106    /// Creates [`CoreError::ParsingNumber`].
107    pub fn parsing_number(
108        inner: &'static str,
109        e: impl std::error::Error + 'static + Send + Sync,
110    ) -> Self {
111        Self::Core(CoreError::ParsingNumber(inner, Box::new(e)))
112    }
113
114    /// Creates [`CoreError::FailedToInitializeHmac`].
115    pub fn failed_to_initialize_hmac(e: impl std::error::Error + 'static + Send + Sync) -> Self {
116        Self::Core(CoreError::FailedToInitializeHmac(Box::new(e)))
117    }
118
119    /// Creates [`ServerError::FailedToBindSocket`].
120    pub fn failed_to_bind_socket(e: impl std::error::Error + 'static + Send + Sync) -> Self {
121        Self::Server(ServerError::FailedToBindSocket(Box::new(e)))
122    }
123
124    /// Creates [`ServerError::FailedToServe`].
125    pub fn failed_to_serve(e: impl std::error::Error + 'static + Send + Sync) -> Self {
126        Self::Server(ServerError::FailedToServe(Box::new(e)))
127    }
128}