From b7442e5e11febf959f95dde517532443f907fe72 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Mon, 8 Apr 2024 00:12:21 +0200 Subject: [PATCH] make it possible to return responses as the 'body' --- Cargo.lock | 1 + crates/kitsune-error/Cargo.toml | 1 + crates/kitsune-error/src/axum.rs | 13 +++++++------ crates/kitsune-error/src/lib.rs | 10 ++++++---- .../mastodon/api/v1/accounts/update_credentials.rs | 2 +- kitsune/src/http/handler/mastodon/api/v1/media.rs | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de85b5b0f..28ae90280 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3823,6 +3823,7 @@ dependencies = [ "garde", "http 1.1.0", "simd-json", + "sync_wrapper 1.0.0", "tracing", ] diff --git a/crates/kitsune-error/Cargo.toml b/crates/kitsune-error/Cargo.toml index 9a6b9aa8b..1af759c7b 100644 --- a/crates/kitsune-error/Cargo.toml +++ b/crates/kitsune-error/Cargo.toml @@ -11,6 +11,7 @@ eyre = "0.6.12" garde = { version = "0.18.0", default-features = false, features = ["serde"] } http = "1.1.0" simd-json = "0.13.9" +sync_wrapper = "1.0.0" tracing = "0.1.40" [lints] diff --git a/crates/kitsune-error/src/axum.rs b/crates/kitsune-error/src/axum.rs index 2caecd7f4..8c04c9e31 100644 --- a/crates/kitsune-error/src/axum.rs +++ b/crates/kitsune-error/src/axum.rs @@ -33,15 +33,16 @@ impl IntoResponse for Error { return to_response(StatusCode::BAD_REQUEST, Some(body)); } + let maybe_body = self.ctx.body.into_inner(); match self.ctx.ty { - ErrorType::BadRequest => to_response(StatusCode::BAD_REQUEST, self.ctx.body), - ErrorType::Forbidden => to_response(StatusCode::FORBIDDEN, self.ctx.body), - ErrorType::NotFound => to_response(StatusCode::NOT_FOUND, self.ctx.body), - ErrorType::Unauthorized => to_response(StatusCode::UNAUTHORIZED, self.ctx.body), + ErrorType::BadRequest => to_response(StatusCode::BAD_REQUEST, maybe_body), + ErrorType::Forbidden => to_response(StatusCode::FORBIDDEN, maybe_body), + ErrorType::NotFound => to_response(StatusCode::NOT_FOUND, maybe_body), + ErrorType::Unauthorized => to_response(StatusCode::UNAUTHORIZED, maybe_body), ErrorType::UnsupportedMediaType => { - to_response(StatusCode::UNSUPPORTED_MEDIA_TYPE, self.ctx.body) + to_response(StatusCode::UNSUPPORTED_MEDIA_TYPE, maybe_body) } - ErrorType::Other => to_response(StatusCode::INTERNAL_SERVER_ERROR, self.ctx.body), + ErrorType::Other => to_response(StatusCode::INTERNAL_SERVER_ERROR, maybe_body), } } } diff --git a/crates/kitsune-error/src/lib.rs b/crates/kitsune-error/src/lib.rs index afd58e20f..8134d73ac 100644 --- a/crates/kitsune-error/src/lib.rs +++ b/crates/kitsune-error/src/lib.rs @@ -1,7 +1,9 @@ #[macro_use] extern crate tracing; +use axum_core::response::{IntoResponse, Response}; use std::fmt::{self, Debug, Display}; +use sync_wrapper::SyncWrapper; pub use self::ext::ResultExt; @@ -43,11 +45,11 @@ impl ErrorType { #[allow(clippy::needless_pass_by_value)] pub fn with_body(self, body: B) -> ErrorContext where - B: ToString, + B: IntoResponse, { ErrorContext { ty: self, - body: Some(body.to_string()), + body: Some(body.into_response()).into(), } } } @@ -56,7 +58,7 @@ impl From for ErrorContext { fn from(value: ErrorType) -> Self { Self { ty: value, - body: None, + body: SyncWrapper::new(None), } } } @@ -64,7 +66,7 @@ impl From for ErrorContext { #[derive(Debug)] pub struct ErrorContext { ty: ErrorType, - body: Option, + body: SyncWrapper>, } #[derive(Debug)] diff --git a/kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs b/kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs index a93c92149..687caf532 100644 --- a/kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs +++ b/kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs @@ -77,7 +77,7 @@ pub async fn patch( let update = update.build().map_err(|err| { kitsune_error!( - type = ErrorType::BadRequest.with_body(err), + type = ErrorType::BadRequest.with_body(err.to_string()), "missing upload field" ) })?; diff --git a/kitsune/src/http/handler/mastodon/api/v1/media.rs b/kitsune/src/http/handler/mastodon/api/v1/media.rs index 35707d532..b7dd8b267 100644 --- a/kitsune/src/http/handler/mastodon/api/v1/media.rs +++ b/kitsune/src/http/handler/mastodon/api/v1/media.rs @@ -89,7 +89,7 @@ pub async fn post( let upload = upload.build().map_err(|err| { kitsune_error!( - type = ErrorType::BadRequest.with_body(err), + type = ErrorType::BadRequest.with_body(err.to_string()), "not all fields were filled" ) })?;