Skip to content

Commit

Permalink
make it possible to return responses as the 'body'
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Apr 7, 2024
1 parent 3b3c34a commit ed555ea
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/kitsune-error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 7 additions & 6 deletions crates/kitsune-error/src/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Check warning on line 41 in crates/kitsune-error/src/axum.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/axum.rs#L33-L41

Added lines #L33 - L41 were not covered by tests
ErrorType::UnsupportedMediaType => {
to_response(StatusCode::UNSUPPORTED_MEDIA_TYPE, self.ctx.body)
to_response(StatusCode::UNSUPPORTED_MEDIA_TYPE, maybe_body)

Check warning on line 43 in crates/kitsune-error/src/axum.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/axum.rs#L43

Added line #L43 was not covered by tests
}
ErrorType::Other => to_response(StatusCode::INTERNAL_SERVER_ERROR, self.ctx.body),
ErrorType::Other => to_response(StatusCode::INTERNAL_SERVER_ERROR, maybe_body),

Check warning on line 45 in crates/kitsune-error/src/axum.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/axum.rs#L45

Added line #L45 was not covered by tests
}
}

Check warning on line 47 in crates/kitsune-error/src/axum.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/axum.rs#L47

Added line #L47 was not covered by tests
}
11 changes: 6 additions & 5 deletions crates/kitsune-error/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -40,14 +42,13 @@ pub enum ErrorType {

impl ErrorType {
#[must_use]
#[allow(clippy::needless_pass_by_value)]
pub fn with_body<B>(self, body: B) -> ErrorContext
where
B: ToString,
B: IntoResponse,
{
ErrorContext {
ty: self,
body: Some(body.to_string()),
body: Some(body.into_response()).into(),
}
}

Check warning on line 53 in crates/kitsune-error/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/lib.rs#L45-L53

Added lines #L45 - L53 were not covered by tests
}
Expand All @@ -56,15 +57,15 @@ impl From<ErrorType> for ErrorContext {
fn from(value: ErrorType) -> Self {
Self {
ty: value,
body: None,
body: SyncWrapper::new(None),
}
}
}

#[derive(Debug)]
pub struct ErrorContext {
ty: ErrorType,
body: Option<String>,
body: SyncWrapper<Option<Response>>,
}

#[derive(Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})?;

Check warning on line 83 in kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs#L78-L83

Added lines #L78 - L83 were not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion kitsune/src/http/handler/mastodon/api/v1/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})?;

Check warning on line 95 in kitsune/src/http/handler/mastodon/api/v1/media.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/media.rs#L90-L95

Added lines #L90 - L95 were not covered by tests
Expand Down

0 comments on commit ed555ea

Please sign in to comment.