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

## [Unreleased]

## [0.20.1] - 2025-10-01

### Changed
- Enriched converter metadata across `multipart`, `redis`, `reqwest`,
`serde_json` and `sqlx` integrations to surface HTTP status details,
retry-after hints and structured failure positions while keeping existing
error categories intact.
- Updated the Teloxide mapping to classify `ApiError::InvalidToken` as
`Unauthorized` and hash potentially sensitive network error details before
emitting telemetry.

### Tests
- Extended integration tests to assert the new metadata fields, retry hints,
and redaction policies covering the updated converters.

## [0.20.0] - 2025-09-30

### 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.20.0"
version = "0.20.1"
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 @@ -38,9 +38,9 @@ guides, comparisons with `thiserror`/`anyhow`, and troubleshooting recipes.

~~~toml
[dependencies]
masterror = { version = "0.20.0", default-features = false }
masterror = { version = "0.20.1", default-features = false }
# or with features:
# masterror = { version = "0.20.0", features = [
# masterror = { version = "0.20.1", features = [
# "axum", "actix", "openapi", "serde_json",
# "tracing", "metrics", "backtrace", "sqlx",
# "sqlx-migrate", "reqwest", "redis", "validator",
Expand Down Expand Up @@ -78,10 +78,10 @@ masterror = { version = "0.20.0", default-features = false }
~~~toml
[dependencies]
# lean core
masterror = { version = "0.20.0", default-features = false }
masterror = { version = "0.20.1", default-features = false }

# with Axum/Actix + JSON + integrations
# masterror = { version = "0.20.0", features = [
# masterror = { version = "0.20.1", features = [
# "axum", "actix", "openapi", "serde_json",
# "tracing", "metrics", "backtrace", "sqlx",
# "sqlx-migrate", "reqwest", "redis", "validator",
Expand Down Expand Up @@ -720,13 +720,13 @@ assert_eq!(problem.grpc.expect("grpc").name, "UNAUTHENTICATED");
Minimal core:

~~~toml
masterror = { version = "0.20.0", default-features = false }
masterror = { version = "0.20.1", default-features = false }
~~~

API (Axum + JSON + deps):

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

~~~toml
masterror = { version = "0.20.0", features = [
masterror = { version = "0.20.1", features = [
"actix", "serde_json", "openapi",
"sqlx", "reqwest", "redis", "validator", "config", "tokio"
] }
Expand Down
5 changes: 1 addition & 4 deletions src/convert/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
use config::ConfigError;

#[cfg(feature = "config")]
use crate::{
AppErrorKind,
app_error::{Context, Error, field}
};
use crate::{AppErrorKind, Context, Error, field};

#[cfg(feature = "config")]
#[cfg_attr(docsrs, doc(cfg(feature = "config")))]
Expand Down
42 changes: 34 additions & 8 deletions src/convert/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@

use axum::extract::multipart::MultipartError;

use crate::{
AppErrorKind,
app_error::{Context, Error, field}
};
use crate::{AppErrorKind, Context, Error, field};

impl From<MultipartError> for Error {
fn from(err: MultipartError) -> Self {
Context::new(AppErrorKind::BadRequest)
.with(field::str("multipart.reason", err.to_string()))
.into_error(err)
let status = err.status();
let body_text = err.body_text();
let mut context = Context::new(AppErrorKind::BadRequest)
.with(field::str("multipart.reason", body_text))
.with(field::u64("http.status", u64::from(status.as_u16())))
.with(field::bool(
"http.is_client_error",
status.is_client_error()
));

if let Some(reason) = status.canonical_reason() {
context = context.with(field::str("http.status_reason", reason));
}

context.into_error(err)
}
}

Expand Down Expand Up @@ -47,12 +56,29 @@ mod tests {
.expect("extractor");

let err = multipart.next_field().await.expect_err("error");
let status = err.status();
let body_text = err.body_text();
let app_err: Error = err.into();

assert_eq!(app_err.kind, AppErrorKind::BadRequest);
assert_eq!(
app_err.metadata().get("multipart.reason"),
Some(&FieldValue::Str(err.to_string().into()))
Some(&FieldValue::Str(body_text.into()))
);
assert_eq!(
app_err.metadata().get("http.status"),
Some(&FieldValue::U64(u64::from(status.as_u16())))
);
assert_eq!(
app_err.metadata().get("http.status_reason"),
status
.canonical_reason()
.map(|reason| FieldValue::Str(reason.into()))
.as_ref()
);
assert_eq!(
app_err.metadata().get("http.is_client_error"),
Some(&FieldValue::Bool(status.is_client_error()))
);
}
}
13 changes: 9 additions & 4 deletions src/convert/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
use redis::{RedisError, RetryMethod};

#[cfg(feature = "redis")]
use crate::{
AppErrorKind,
app_error::{Context, Error, field}
};
use crate::{AppErrorKind, Context, Error, field};

/// Map any [`redis::RedisError`] into an [`AppError`] with kind `Cache`.
///
Expand Down Expand Up @@ -104,6 +101,10 @@ fn build_context(err: &RedisError) -> (Context, Option<u64>) {
format!("{:?}", retry_method)
));

if let Some(secs) = retry_after {
context = context.with(field::u64("redis.retry_after_hint_secs", secs));
}

(context, retry_after)
}

Expand Down Expand Up @@ -144,5 +145,9 @@ mod tests {
let app_err: Error = err.into();
assert_eq!(app_err.retry.map(|r| r.after_seconds), Some(2));
assert!(matches!(app_err.kind, AppErrorKind::DependencyUnavailable));
assert_eq!(
app_err.metadata().get("redis.retry_after_hint_secs"),
Some(&FieldValue::U64(2))
);
}
}
12 changes: 9 additions & 3 deletions src/convert/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
#[cfg(feature = "reqwest")]
use reqwest::{Error as ReqwestError, StatusCode};

use crate::AppErrorKind;
#[cfg(feature = "reqwest")]
use crate::app_error::{Context, Error, FieldRedaction, field};
use crate::{AppErrorKind, Context, Error, FieldRedaction, field};

/// Map a [`reqwest::Error`] into an [`Error`] according to its category.
///
Expand Down Expand Up @@ -116,6 +114,10 @@ fn classify_reqwest_error(err: &ReqwestError) -> (Context, Option<u64>) {
context = context.with(field::str("http.host", host.to_owned()));
}

if let Some(port) = url.port() {
context = context.with(field::u64("http.port", u64::from(port)));
}

let path = url.path();
if !path.is_empty() {
context = context.with(field::str("http.path", path.to_owned()));
Expand Down Expand Up @@ -205,6 +207,10 @@ mod tests {
assert_eq!(app_err.retry.map(|r| r.after_seconds), Some(1));
let metadata = app_err.metadata();
assert_eq!(metadata.get("http.status"), Some(&FieldValue::U64(429)));
assert_eq!(
metadata.get("http.port"),
Some(&FieldValue::U64(u64::from(addr.port())))
);

server.abort();
}
Expand Down
15 changes: 11 additions & 4 deletions src/convert/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@
use serde_json::{Error as SjError, error::Category};

#[cfg(feature = "serde_json")]
use crate::{
AppErrorKind,
app_error::{Context, Error, field}
};
use crate::{AppErrorKind, Context, Error, field};

/// Map a [`serde_json::Error`] into an [`AppError`].
///
Expand Down Expand Up @@ -71,6 +68,12 @@ fn build_context(err: &SjError) -> Context {
if column != 0 {
context = context.with(field::u64("serde_json.column", u64::from(column)));
}
if line != 0 && column != 0 {
context = context.with(field::str(
"serde_json.position",
format!("{line}:{column}")
));
}

context
}
Expand Down Expand Up @@ -117,5 +120,9 @@ mod tests {
metadata.get("serde_json.category"),
Some(&FieldValue::Str("Syntax".into()))
);
assert_eq!(
metadata.get("serde_json.position"),
Some(&FieldValue::Str("1:1".into()))
);
}
}
17 changes: 12 additions & 5 deletions src/convert/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ use sqlx::migrate::MigrateError;
use sqlx_core::error::{DatabaseError, Error as SqlxError, ErrorKind as SqlxErrorKind};

#[cfg(any(feature = "sqlx", feature = "sqlx-migrate"))]
use crate::{
AppCode, AppErrorKind,
app_error::{Context, Error, field}
};
use crate::{AppCode, AppErrorKind, Context, Error, field};

#[cfg(feature = "sqlx")]
const SQLSTATE_CODE_OVERRIDES: &[(&str, AppCode)] = &[
Expand Down Expand Up @@ -88,7 +85,7 @@ impl From<MigrateError> for Error {

#[cfg(feature = "sqlx")]
fn build_sqlx_context(err: &SqlxError) -> (Context, Option<u64>) {
match err {
let (mut context, retry_after) = match err {
SqlxError::RowNotFound => (
Context::new(AppErrorKind::NotFound).with(field::str("db.reason", "row_not_found")),
None
Expand Down Expand Up @@ -196,7 +193,13 @@ fn build_sqlx_context(err: &SqlxError) -> (Context, Option<u64>) {
.with(field::str("db.detail", format!("{:?}", other))),
None
)
};

if let Some(secs) = retry_after {
context = context.with(field::u64("db.retry_after_hint_secs", secs));
}

(context, retry_after)
}

#[cfg(feature = "sqlx")]
Expand Down Expand Up @@ -346,6 +349,10 @@ mod tests_sqlx {
};
let err: Error = SqlxError::Database(Box::new(db_err)).into();
assert_eq!(err.retry.map(|r| r.after_seconds), Some(1));
assert_eq!(
err.metadata().get("db.retry_after_hint_secs"),
Some(&FieldValue::U64(1))
);
}

#[derive(Debug)]
Expand Down
5 changes: 1 addition & 4 deletions src/convert/telegram_webapp_sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
use telegram_webapp_sdk::utils::validate_init_data::ValidationError;

#[cfg(feature = "telegram-webapp-sdk")]
use crate::{
AppErrorKind,
app_error::{Context, Error, field}
};
use crate::{AppErrorKind, Context, Error, field};

/// Map [`ValidationError`] into an [`AppError`] with kind `TelegramAuth`.
#[cfg(feature = "telegram-webapp-sdk")]
Expand Down
Loading
Loading