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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [0.24.1] - 2025-10-17

### Fixed
- Updated `Context::into_error` to move dynamic `AppCode` values into the
resulting `AppError`, reworking field redaction plumbing to avoid clones and
preserve custom code ownership. Added a regression test covering pointer
identity for context-promoted errors.

## [0.24.0] - 2025-10-16

### Added
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.24.0"
version = "0.24.1"
rust-version = "1.90"
edition = "2024"
license = "MIT OR Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ The build script keeps the full feature snippet below in sync with

~~~toml
[dependencies]
masterror = { version = "0.24.0", default-features = false }
masterror = { version = "0.24.1", default-features = false }
# or with features:
# masterror = { version = "0.24.0", features = [
# masterror = { version = "0.24.1", features = [
# "std", "axum", "actix", "openapi",
# "serde_json", "tracing", "metrics", "backtrace",
# "sqlx", "sqlx-migrate", "reqwest", "redis",
Expand Down
48 changes: 30 additions & 18 deletions src/app_error/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Context {
#[must_use]
pub fn with(mut self, field: Field) -> Self {
self.fields.push(field);
self.apply_field_redactions();
Self::apply_field_redactions(&mut self.fields, &self.field_policies);
self
}

Expand Down Expand Up @@ -132,35 +132,45 @@ impl Context {
self
}

pub(crate) fn into_error<E>(mut self, source: E) -> Error
pub(crate) fn into_error<E>(self, source: E) -> Error
where
E: CoreError + Send + Sync + 'static
{
if let Some(location) = self.caller_location {
self.fields.push(Field::new(
let Context {
mut fields,
field_policies,
edit_policy,
caller_location,
code,
category,
..
} = self;

if let Some(location) = caller_location {
fields.push(Field::new(
"caller.file",
FieldValue::Str(location.file().into())
));
self.fields.push(Field::new(
fields.push(Field::new(
"caller.line",
FieldValue::U64(u64::from(location.line()))
));
self.fields.push(Field::new(
fields.push(Field::new(
"caller.column",
FieldValue::U64(u64::from(location.column()))
));
}

let mut error = AppError::new_raw(self.category, None);
error.code = self.code.clone();
if !self.fields.is_empty() {
self.apply_field_redactions();
error.metadata.extend(self.fields);
let mut error = AppError::new_raw(category, None);
error.code = code;
if !fields.is_empty() {
Self::apply_field_redactions(&mut fields, &field_policies);
error.metadata.extend(fields);
}
for &(name, redaction) in &self.field_policies {
for &(name, redaction) in &field_policies {
error = error.redact_field(name, redaction);
}
if matches!(self.edit_policy, MessageEditPolicy::Redact) {
if matches!(edit_policy, MessageEditPolicy::Redact) {
error.edit_policy = MessageEditPolicy::Redact;
}
let error = error.with_context(source);
Expand All @@ -170,13 +180,15 @@ impl Context {
}

impl Context {
fn apply_field_redactions(&mut self) {
if self.field_policies.is_empty() {
fn apply_field_redactions(
fields: &mut Vec<Field>,
policies: &[(&'static str, FieldRedaction)]
) {
if policies.is_empty() {
return;
}
for field in &mut self.fields {
if let Some((_, policy)) = self
.field_policies
for field in fields {
if let Some((_, policy)) = policies
.iter()
.rev()
.find(|(name, _)| *name == field.name())
Expand Down
15 changes: 14 additions & 1 deletion src/app_error/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static BACKTRACE_ENV_GUARD: Mutex<()> = Mutex::new(());
static TELEMETRY_GUARD: Mutex<()> = Mutex::new(());

use super::{AppError, FieldRedaction, FieldValue, MessageEditPolicy, field};
use crate::{AppCode, AppErrorKind, ErrorResponse};
use crate::{AppCode, AppErrorKind, Context, ErrorResponse, ResultExt};

// --- Helpers -------------------------------------------------------------

Expand Down Expand Up @@ -198,6 +198,19 @@ fn retry_and_www_authenticate_are_attached() {
assert_eq!(err.www_authenticate.as_deref(), Some("Bearer"));
}

#[test]
fn context_moves_dynamic_code_without_cloning() {
let dynamic_code =
AppCode::try_new(String::from("THIRD_PARTY_FAILURE")).expect("valid dynamic code");
let expected_ptr = dynamic_code.as_str().as_ptr();

let err = Result::<(), IoError>::Err(IoError::from(IoErrorKind::Other))
.ctx(|| Context::new(AppErrorKind::Service).code(dynamic_code))
.unwrap_err();

assert_eq!(err.code.as_str().as_ptr(), expected_ptr);
}

#[test]
fn render_message_does_not_allocate_for_borrowed_str() {
let err = AppError::new(AppErrorKind::BadRequest, "borrowed");
Expand Down
7 changes: 3 additions & 4 deletions tests/ui/app_error/fail/enum_missing_variant.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
error: all variants must use #[app_error(...)] to derive AppError conversion
--> tests/ui/app_error/fail/enum_missing_variant.rs:8:5
|
8 | / #[error("without")]
9 | | Without,
| |___________^
8 | #[error("without")]
| ^

warning: unused import: `AppErrorKind`
--> tests/ui/app_error/fail/enum_missing_variant.rs:1:17
|
1 | use masterror::{AppErrorKind, Error};
| ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
= note: `#[warn(unused_imports)]` on by default
4 changes: 2 additions & 2 deletions tests/ui/app_error/fail/missing_code.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ error: AppCode conversion requires `code = ...` in #[app_error(...)]
--> tests/ui/app_error/fail/missing_code.rs:9:5
|
9 | #[app_error(kind = AppErrorKind::Service)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^

warning: unused imports: `AppCode` and `AppErrorKind`
--> tests/ui/app_error/fail/missing_code.rs:1:17
|
1 | use masterror::{AppCode, AppErrorKind, Error};
| ^^^^^^^ ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
= note: `#[warn(unused_imports)]` on by default
2 changes: 1 addition & 1 deletion tests/ui/app_error/fail/missing_kind.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ error: missing `kind = ...` in #[app_error(...)]
--> tests/ui/app_error/fail/missing_kind.rs:5:1
|
5 | #[app_error(message)]
| ^^^^^^^^^^^^^^^^^^^^^
| ^
2 changes: 1 addition & 1 deletion tests/ui/formatter/fail/duplicate_fmt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ error: duplicate `fmt` handler specified
--> tests/ui/formatter/fail/duplicate_fmt.rs:4:36
|
4 | #[error(fmt = crate::format_error, fmt = crate::format_error)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^
1 change: 0 additions & 1 deletion tests/ui/formatter/fail/implicit_after_named.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ error: multiple unused formatting arguments
| argument never used
| argument never used
|
= note: consider adding 2 format specifiers
= note: this error originates in the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)
4 changes: 2 additions & 2 deletions tests/ui/formatter/fail/unsupported_flag.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: placeholder spanning bytes 0..11 uses an unsupported formatter
--> tests/ui/formatter/fail/unsupported_flag.rs:4:10
--> tests/ui/formatter/fail/unsupported_flag.rs:4:9
|
4 | #[error("{value:##x}")]
| ^^^^^^^^^^^
| ^^^^^^^^^^^^^
4 changes: 2 additions & 2 deletions tests/ui/formatter/fail/unsupported_formatter.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: placeholder spanning bytes 0..9 uses an unsupported formatter
--> tests/ui/formatter/fail/unsupported_formatter.rs:4:10
--> tests/ui/formatter/fail/unsupported_formatter.rs:4:9
|
4 | #[error("{value:y}")]
| ^^^^^^^^^
| ^^^^^^^^^^^
4 changes: 2 additions & 2 deletions tests/ui/formatter/fail/uppercase_binary.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: placeholder spanning bytes 0..9 uses an unsupported formatter
--> tests/ui/formatter/fail/uppercase_binary.rs:4:10
--> tests/ui/formatter/fail/uppercase_binary.rs:4:9
|
4 | #[error("{value:B}")]
| ^^^^^^^^^
| ^^^^^^^^^^^
4 changes: 2 additions & 2 deletions tests/ui/formatter/fail/uppercase_pointer.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: placeholder spanning bytes 0..9 uses an unsupported formatter
--> tests/ui/formatter/fail/uppercase_pointer.rs:4:10
--> tests/ui/formatter/fail/uppercase_pointer.rs:4:9
|
4 | #[error("{value:P}")]
| ^^^^^^^^^
| ^^^^^^^^^^^
2 changes: 1 addition & 1 deletion tests/ui/masterror/fail/duplicate_attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ warning: unused imports: `AppCode` and `AppErrorKind`
1 | use masterror::{AppCode, AppErrorKind, Masterror};
| ^^^^^^^ ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
= note: `#[warn(unused_imports)]` on by default
2 changes: 1 addition & 1 deletion tests/ui/masterror/fail/duplicate_telemetry.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ warning: unused imports: `AppCode` and `AppErrorKind`
1 | use masterror::{AppCode, AppErrorKind, Masterror};
| ^^^^^^^ ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
= note: `#[warn(unused_imports)]` on by default
2 changes: 1 addition & 1 deletion tests/ui/masterror/fail/empty_redact.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ warning: unused imports: `AppCode` and `AppErrorKind`
1 | use masterror::{AppCode, AppErrorKind, Masterror};
| ^^^^^^^ ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
= note: `#[warn(unused_imports)]` on by default
7 changes: 3 additions & 4 deletions tests/ui/masterror/fail/enum_missing_variant.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
error: all variants must use #[masterror(...)] to derive masterror::Error conversion
--> tests/ui/masterror/fail/enum_missing_variant.rs:8:5
|
8 | / #[error("missing")]
9 | | Missing
| |___________^
8 | #[error("missing")]
| ^

warning: unused imports: `AppCode` and `AppErrorKind`
--> tests/ui/masterror/fail/enum_missing_variant.rs:1:17
|
1 | use masterror::{AppCode, AppErrorKind, Masterror};
| ^^^^^^^ ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
= note: `#[warn(unused_imports)]` on by default
4 changes: 2 additions & 2 deletions tests/ui/masterror/fail/missing_category.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ error: missing `category = ...` in #[masterror(...)]
--> tests/ui/masterror/fail/missing_category.rs:5:1
|
5 | #[masterror(code = AppCode::Internal)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^

warning: unused import: `AppCode`
--> tests/ui/masterror/fail/missing_category.rs:1:17
|
1 | use masterror::{AppCode, Masterror};
| ^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
= note: `#[warn(unused_imports)]` on by default
4 changes: 2 additions & 2 deletions tests/ui/masterror/fail/missing_code.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ error: missing `code = ...` in #[masterror(...)]
--> tests/ui/masterror/fail/missing_code.rs:5:1
|
5 | #[masterror(category = AppErrorKind::Internal)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^

warning: unused import: `AppErrorKind`
--> tests/ui/masterror/fail/missing_code.rs:1:17
|
1 | use masterror::{AppErrorKind, Masterror};
| ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
= note: `#[warn(unused_imports)]` on by default
2 changes: 1 addition & 1 deletion tests/ui/masterror/fail/unknown_option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ warning: unused imports: `AppCode` and `AppErrorKind`
1 | use masterror::{AppCode, AppErrorKind, Masterror};
| ^^^^^^^ ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
= note: `#[warn(unused_imports)]` on by default
Loading