slack_hook/
error.rs

1use std::{fmt, str::Utf8Error};
2
3use hex::FromHexError;
4
5/// An alias for a `Result` with a `slack_hook::Error`
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// The all-encompassing error type for the `slack-hook` crate
9#[derive(Debug)]
10pub enum Error {
11    /// slack service error
12    Slack(String),
13    /// Hex color parsing error
14    HexColor(String),
15    /// utf8 error, slack responses should be valid utf8
16    Utf8(Utf8Error),
17    /// `serde_json::Error`
18    Serialize(serde_json::Error),
19    /// `hex::FromHexError`
20    FromHex(FromHexError),
21    /// `reqwest::Error`
22    Reqwest(reqwest::Error),
23    /// `url::ParseError`
24    Url(url::ParseError),
25    /// `std::io::Error`
26    Io(std::io::Error),
27}
28
29impl From<Utf8Error> for Error {
30    fn from(utf8_err: Utf8Error) -> Self {
31        Self::Utf8(utf8_err)
32    }
33}
34
35impl From<serde_json::Error> for Error {
36    fn from(json_err: serde_json::Error) -> Self {
37        Self::Serialize(json_err)
38    }
39}
40
41impl From<FromHexError> for Error {
42    fn from(hex_err: FromHexError) -> Self {
43        Self::FromHex(hex_err)
44    }
45}
46
47impl From<reqwest::Error> for Error {
48    fn from(reqwest_err: reqwest::Error) -> Self {
49        Self::Reqwest(reqwest_err)
50    }
51}
52
53impl From<url::ParseError> for Error {
54    fn from(url_err: url::ParseError) -> Self {
55        Self::Url(url_err)
56    }
57}
58
59impl From<std::io::Error> for Error {
60    fn from(io_err: std::io::Error) -> Self {
61        Self::Io(io_err)
62    }
63}
64
65impl fmt::Display for Error {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        match self {
68            Self::Slack(err) => write!(f, "slack service error: {err}"),
69            Self::HexColor(err) => write!(f, "hex color parsing error: {err}"),
70            Self::Utf8(err) => err.fmt(f),
71            Self::Serialize(err) => err.fmt(f),
72            Self::FromHex(err) => err.fmt(f),
73            Self::Reqwest(err) => err.fmt(f),
74            Self::Url(err) => err.fmt(f),
75            Self::Io(err) => err.fmt(f),
76        }
77    }
78}
79
80impl std::error::Error for Error {}