diff options
author | cel 🌸 <cel@blos.sm> | 2024-01-30 16:16:05 +0000 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2024-01-30 16:16:05 +0000 |
commit | f00159f53b3774601500ec65345791311ff6efa1 (patch) | |
tree | cc65a487e71a056126e1935108a494c7851863f9 /src/error.rs | |
parent | 8c03d9a53f4bc2f70fb5c1e0487bc74fe0137fcb (diff) | |
download | blossom-f00159f53b3774601500ec65345791311ff6efa1.tar.gz blossom-f00159f53b3774601500ec65345791311ff6efa1.tar.bz2 blossom-f00159f53b3774601500ec65345791311ff6efa1.zip |
migrate to poem and askama
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 88 |
1 files changed, 67 insertions, 21 deletions
diff --git a/src/error.rs b/src/error.rs index 42c01b1..611ce79 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,66 +1,112 @@ use std::string::FromUtf8Error; -use rocket::{http::Status, Responder}; +use poem::{error::ResponseError, http::StatusCode, IntoResponse}; +use thiserror::Error; -#[derive(Responder, Debug)] +use crate::templates; + +#[derive(Error, Debug)] pub enum BlossomError { - Reqwest(Status, #[response(ignore)] reqwest::Error), - ListenBrainz(Status, #[response(ignore)] listenbrainz::Error), - Skinnyverse(Status, #[response(ignore)] mastodon_async::Error), - Chrono(Status, #[response(ignore)] chrono::ParseError), - Io(Status, #[response(ignore)] std::io::Error), - Deserialization(Status, #[response(ignore)] toml::de::Error), - Syndicator(Status, #[response(ignore)] atom_syndication::Error), - Utf8Conversion(Status, #[response(ignore)] FromUtf8Error), - NotFound(Status), - NoMetadata(Status), - Unimplemented(Status), + #[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 { + status: self.status(), + message: self.to_string(), + } + .into_response() + } } impl From<reqwest::Error> for BlossomError { fn from(e: reqwest::Error) -> Self { - BlossomError::Reqwest(Status::new(500), e) + BlossomError::Reqwest(e) } } impl From<listenbrainz::Error> for BlossomError { fn from(e: listenbrainz::Error) -> Self { - BlossomError::ListenBrainz(Status::new(500), e) + BlossomError::ListenBrainz(e) } } impl From<mastodon_async::Error> for BlossomError { fn from(e: mastodon_async::Error) -> Self { - BlossomError::Skinnyverse(Status::new(500), e) + BlossomError::Skinnyverse(e) } } impl From<chrono::ParseError> for BlossomError { fn from(e: chrono::ParseError) -> Self { - BlossomError::Chrono(Status::new(500), e) + BlossomError::Chrono(e) } } impl From<std::io::Error> for BlossomError { fn from(e: std::io::Error) -> Self { - BlossomError::Io(Status::new(500), e) + BlossomError::Io(e) } } impl From<toml::de::Error> for BlossomError { fn from(e: toml::de::Error) -> Self { - BlossomError::Deserialization(Status::new(500), e) + BlossomError::Deserialization(e) } } impl From<atom_syndication::Error> for BlossomError { fn from(e: atom_syndication::Error) -> Self { - BlossomError::Syndicator(Status::new(500), e) + BlossomError::Syndicator(e) } } impl From<FromUtf8Error> for BlossomError { fn from(e: FromUtf8Error) -> Self { - BlossomError::Utf8Conversion(Status::new(500), e) + BlossomError::Utf8Conversion(e) } } |