use std::{
fmt::{self, Display},
io,
};
use reqwest::{self, StatusCode, header::InvalidHeaderValue};
use thiserror::Error as ThisError;
#[derive(Debug)]
pub struct RequestNotSuccessful {
pub status: StatusCode,
pub body: String,
}
impl RequestNotSuccessful {
pub fn new(status: StatusCode, body: String) -> Self {
Self { status, body }
}
}
impl std::error::Error for RequestNotSuccessful {}
impl Display for RequestNotSuccessful {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "StatusCode: {}, Body: {}", self.status, self.body)
}
}
#[derive(ThisError, Debug)]
pub enum SendgridError {
#[error("IO Error: `{0}`")]
Io(#[from] io::Error),
#[error("JSON Error: `{0}`")]
JSONDecode(#[from] serde_json::Error),
#[error("HTTP Error: `{0}`")]
ReqwestError(#[from] reqwest::Error),
#[error("Invalid Header Error: `{0}`")]
InvalidHeader(#[from] InvalidHeaderValue),
#[error("could not UTF-8 decode this filename")]
InvalidFilename,
#[error("dynamic template data must be a serializable object")]
InvalidTemplateValue,
#[error("the number of items exceeded the max capacity")]
TooManyItems,
#[error("Request failed: `{0}`")]
RequestNotSuccessful(#[from] RequestNotSuccessful),
}
pub type SendgridResult<T> = Result<T, SendgridError>;