use std::string::FromUtf8Error; use poem::{error::ResponseError, http::StatusCode, IntoResponse}; use thiserror::Error; use crate::templates; #[derive(Error, Debug)] pub enum BlossomError { #[error("http client error")] Reqwest(reqwest::Error), #[error("listenbrainz error")] ListenBrainz(listenbrainz::Error), #[error("mastadon error")] Skinnyverse(mastodon_async::Error), #[error("failed to parse timestamp")] Chrono(chrono::ParseError), #[error("io error")] Io(std::io::Error), #[error("toml deserialization error")] Deserialization(toml::de::Error), #[error("atom syndication error")] Syndicator(atom_syndication::Error), #[error("utf8 conversion error")] Utf8Conversion(FromUtf8Error), #[error("not found")] NotFound, #[error("no metadata")] NoMetadata, #[error("unimplemented")] Unimplemented, #[error("not a file")] NotAFile, } impl ResponseError for BlossomError { fn status(&self) -> poem::http::StatusCode { match self { BlossomError::Reqwest(_) => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::ListenBrainz(_) => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::Skinnyverse(_) => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::Chrono(_) => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::Io(_) => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::Deserialization(_) => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::Syndicator(_) => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::Utf8Conversion(_) => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::NotFound => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::NoMetadata => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::Unimplemented => StatusCode::INTERNAL_SERVER_ERROR, BlossomError::NotAFile => StatusCode::INTERNAL_SERVER_ERROR, } } fn as_response(&self) -> poem::Response where Self: std::error::Error + Send + Sync + 'static, { templates::Error { title: self.to_string(), status: self.status(), message: self.to_string(), } .into_response() } } impl From for BlossomError { fn from(e: reqwest::Error) -> Self { BlossomError::Reqwest(e) } } impl From for BlossomError { fn from(e: listenbrainz::Error) -> Self { BlossomError::ListenBrainz(e) } } impl From for BlossomError { fn from(e: mastodon_async::Error) -> Self { BlossomError::Skinnyverse(e) } } impl From for BlossomError { fn from(e: chrono::ParseError) -> Self { BlossomError::Chrono(e) } } impl From for BlossomError { fn from(e: std::io::Error) -> Self { BlossomError::Io(e) } } impl From for BlossomError { fn from(e: toml::de::Error) -> Self { BlossomError::Deserialization(e) } } impl From for BlossomError { fn from(e: atom_syndication::Error) -> Self { BlossomError::Syndicator(e) } } impl From for BlossomError { fn from(e: FromUtf8Error) -> Self { BlossomError::Utf8Conversion(e) } }