plausible_rs/
error.rs

1use bytes::Bytes;
2use reqwest::StatusCode;
3use std::fmt::{Debug, Formatter};
4use std::{error, fmt};
5
6#[derive(Debug)]
7pub enum Error {
8    /// Error occurred while using the `reqwest` library.
9    ReqwestError(reqwest::Error),
10
11    /// An API request returned with a failed status code.
12    RequestFailed {
13        bytes: Bytes,
14        status_code: StatusCode,
15    },
16
17    /// Error occurred while using the `serde` library.
18    SerdeError(serde_json::Error),
19}
20
21impl error::Error for Error {}
22
23impl fmt::Display for Error {
24    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::ReqwestError(e) => std::fmt::Display::fmt(&e, f),
27            Self::RequestFailed { bytes, status_code } => {
28                let text = String::from_utf8_lossy(bytes);
29                write!(f, "{status_code}: {text}")
30            }
31            Self::SerdeError(e) => write!(f, "{e}"),
32        }
33    }
34}
35
36impl From<reqwest::Error> for Error {
37    fn from(e: reqwest::Error) -> Self {
38        Self::ReqwestError(e)
39    }
40}
41
42impl From<serde_json::Error> for Error {
43    fn from(e: serde_json::Error) -> Self {
44        Self::SerdeError(e)
45    }
46}