diff options
author | Vika <vika@fireburn.ru> | 2022-07-10 14:52:43 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2022-07-10 14:52:43 +0300 |
commit | 3a7af37527c7752b42d518ec719a479254d6ba96 (patch) | |
tree | 5a51a8167d31330bd6af0f82858160a9e2759c6c /kittybox-rs/util/src | |
parent | 25183f2ed7802375f15cb0069af7bee6dd2c7afd (diff) | |
download | kittybox-3a7af37527c7752b42d518ec719a479254d6ba96.tar.zst |
micropub: move MicropubError into kittybox-util
Looks like this shared data structure will be useful to me later when splitting off the media endpoint into its own crate.
Diffstat (limited to 'kittybox-rs/util/src')
-rw-r--r-- | kittybox-rs/util/src/error.rs | 90 | ||||
-rw-r--r-- | kittybox-rs/util/src/lib.rs | 3 |
2 files changed, 93 insertions, 0 deletions
diff --git a/kittybox-rs/util/src/error.rs b/kittybox-rs/util/src/error.rs new file mode 100644 index 0000000..79f43ef --- /dev/null +++ b/kittybox-rs/util/src/error.rs @@ -0,0 +1,90 @@ +use serde::{Deserialize, Serialize}; +use http::StatusCode; +use axum_core::response::{Response, IntoResponse}; + +#[derive(Serialize, Deserialize, PartialEq, Debug)] +#[serde(rename_all = "snake_case")] +pub enum ErrorType { + /// An erroneous attempt to create something that already exists. + AlreadyExists, + /// Current user is expressly forbidden from performing this action. + Forbidden, + /// The Micropub server experienced an internal error. + InternalServerError, + /// The request was invalid or malformed. + InvalidRequest, + /// The provided OAuth2 scopes were insufficient to allow performing this action. + InvalidScope, + /// There was no token or other means of authorization in the request. + NotAuthorized, + /// Whatever was requested was not found. + NotFound, + /// The request payload was of a type unsupported by the Micropub endpoint. + UnsupportedMediaType, +} + +/// Representation of the Micropub API error. +#[derive(Serialize, Deserialize, Debug)] +pub struct MicropubError { + pub error: ErrorType, + pub error_description: String, +} + +impl std::error::Error for MicropubError {} + +impl std::fmt::Display for MicropubError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("Micropub error: ")?; + f.write_str(&self.error_description) + } +} + +impl From<serde_json::Error> for MicropubError { + fn from(err: serde_json::Error) -> Self { + use ErrorType::*; + Self { + error: InvalidRequest, + error_description: err.to_string(), + } + } +} + +impl MicropubError { + pub fn new(error: ErrorType, error_description: &str) -> Self { + Self { + error, + error_description: error_description.to_owned(), + } + } +} + +impl From<&MicropubError> for StatusCode { + fn from(err: &MicropubError) -> Self { + use ErrorType::*; + match err.error { + AlreadyExists => StatusCode::CONFLICT, + Forbidden => StatusCode::FORBIDDEN, + InternalServerError => StatusCode::INTERNAL_SERVER_ERROR, + InvalidRequest => StatusCode::BAD_REQUEST, + InvalidScope => StatusCode::UNAUTHORIZED, + NotAuthorized => StatusCode::UNAUTHORIZED, + NotFound => StatusCode::NOT_FOUND, + UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE, + } + } +} +impl From<MicropubError> for StatusCode { + fn from(err: MicropubError) -> Self { + (&err).into() + } +} + +impl IntoResponse for MicropubError { + fn into_response(self) -> Response { + IntoResponse::into_response(( + StatusCode::from(&self), + [("Content-Type", "application/json")], + serde_json::to_string(&self).unwrap(), + )) + } +} diff --git a/kittybox-rs/util/src/lib.rs b/kittybox-rs/util/src/lib.rs index bc41689..debe589 100644 --- a/kittybox-rs/util/src/lib.rs +++ b/kittybox-rs/util/src/lib.rs @@ -16,3 +16,6 @@ pub struct MicropubChannel { /// The channel's user-friendly name used to recognize it in lists. pub name: String, } + +pub mod error; +pub use error::{ErrorType, MicropubError}; |