Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [0.10.7] - 2025-09-22

### Changed
- Added an owning `From<AppError>` conversion for `ErrorResponse` and updated the
Axum adapter to use it, eliminating redundant clones when building HTTP error
bodies.

## [0.10.6] - 2025-09-21

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "masterror"
version = "0.10.6"
version = "0.10.7"
rust-version = "1.90"
edition = "2024"
license = "MIT OR Apache-2.0"
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Stable categories, conservative HTTP mapping, no `unsafe`.

~~~toml
[dependencies]
masterror = { version = "0.10.6", default-features = false }
masterror = { version = "0.10.7", default-features = false }
# or with features:
# masterror = { version = "0.10.6", features = [
# masterror = { version = "0.10.7", features = [
# "axum", "actix", "openapi", "serde_json",
# "sqlx", "sqlx-migrate", "reqwest", "redis",
# "validator", "config", "tokio", "multipart",
Expand Down Expand Up @@ -66,10 +66,10 @@ masterror = { version = "0.10.6", default-features = false }
~~~toml
[dependencies]
# lean core
masterror = { version = "0.10.6", default-features = false }
masterror = { version = "0.10.7", default-features = false }

# with Axum/Actix + JSON + integrations
# masterror = { version = "0.10.6", features = [
# masterror = { version = "0.10.7", features = [
# "axum", "actix", "openapi", "serde_json",
# "sqlx", "sqlx-migrate", "reqwest", "redis",
# "validator", "config", "tokio", "multipart",
Expand Down Expand Up @@ -623,13 +623,13 @@ assert_eq!(resp.status, 401);
Minimal core:

~~~toml
masterror = { version = "0.10.6", default-features = false }
masterror = { version = "0.10.7", default-features = false }
~~~

API (Axum + JSON + deps):

~~~toml
masterror = { version = "0.10.6", features = [
masterror = { version = "0.10.7", features = [
"axum", "serde_json", "openapi",
"sqlx", "reqwest", "redis", "validator", "config", "tokio"
] }
Expand All @@ -638,7 +638,7 @@ masterror = { version = "0.10.6", features = [
API (Actix + JSON + deps):

~~~toml
masterror = { version = "0.10.6", features = [
masterror = { version = "0.10.7", features = [
"actix", "serde_json", "openapi",
"sqlx", "reqwest", "redis", "validator", "config", "tokio"
] }
Expand Down
2 changes: 1 addition & 1 deletion src/convert/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl IntoResponse for AppError {
#[cfg(feature = "serde_json")]
{
// Build the stable wire contract (includes `code`).
let body: ErrorResponse = (&self).into();
let body: ErrorResponse = self.into();
return body.into_response();
}

Expand Down
27 changes: 27 additions & 0 deletions src/response/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ impl Display for ErrorResponse {
}
}

impl From<AppError> for ErrorResponse {
fn from(err: AppError) -> Self {
let AppError {
kind,
message,
retry,
www_authenticate
} = err;

let status = kind.http_status();
let code = AppCode::from(kind);
let message = match message {
Some(msg) => msg.into_owned(),
None => String::from("An error occurred")
};

Self {
status,
code,
message,
details: None,
retry,
www_authenticate
}
}
}

impl From<&AppError> for ErrorResponse {
fn from(err: &AppError) -> Self {
let status = err.kind.http_status();
Expand Down
24 changes: 24 additions & 0 deletions src/response/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,30 @@ fn from_app_error_uses_default_message_when_none() {
assert_eq!(e.message, "An error occurred");
}

#[test]
fn from_owned_app_error_moves_message_and_metadata() {
let err = AppError::unauthorized(String::from("owned message"))
.with_retry_after_secs(5)
.with_www_authenticate("Bearer");

let resp: ErrorResponse = err.into();

assert_eq!(resp.status, 401);
assert!(matches!(resp.code, AppCode::Unauthorized));
assert_eq!(resp.message, "owned message");
assert_eq!(resp.retry.unwrap().after_seconds, 5);
assert_eq!(resp.www_authenticate.as_deref(), Some("Bearer"));
}

#[test]
fn from_owned_app_error_defaults_message_when_absent() {
let resp: ErrorResponse = AppError::bare(AppErrorKind::Internal).into();

assert_eq!(resp.status, 500);
assert!(matches!(resp.code, AppCode::Internal));
assert_eq!(resp.message, "An error occurred");
}

// --- Display formatting --------------------------------------------------

#[test]
Expand Down
Loading