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

## [Unreleased]

## [0.20.5] - 2025-10-05

### Fixed
- Promoted the gRPC converter to an infallible `From<Error>` implementation
while retaining the `TryFrom` API via the new documented
`StatusConversionError`, satisfying Clippy's infallible conversion lint.
- Collapsed nested metadata guards in the Tonic adapter and reused borrowed
booleans to silence Clippy without regressing runtime behaviour.
- Simplified the `AppResult` alias test to avoid large `Err` variant warnings
from Clippy's `result_large_err` lint.

## [0.20.4] - 2025-10-04

### 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.4"
version = "0.20.5"
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.4", default-features = false }
masterror = { version = "0.20.5", default-features = false }
# or with features:
# masterror = { version = "0.20.4", features = [
# masterror = { version = "0.20.5", 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.4", default-features = false }
~~~toml
[dependencies]
# lean core
masterror = { version = "0.20.4", default-features = false }
masterror = { version = "0.20.5", default-features = false }

# with Axum/Actix + JSON + integrations
# masterror = { version = "0.20.4", features = [
# masterror = { version = "0.20.5", 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.4", default-features = false }
masterror = { version = "0.20.5", default-features = false }
~~~

API (Axum + JSON + deps):

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

~~~toml
masterror = { version = "0.20.4", features = [
masterror = { version = "0.20.5", features = [
"actix", "serde_json", "openapi",
"sqlx", "reqwest", "redis", "validator", "config", "tokio"
] }
Expand Down
13 changes: 4 additions & 9 deletions src/app_error/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,9 @@ fn metrics_counter_is_incremented_once() {

#[test]
fn result_alias_is_generic() {
fn app() -> super::AppResult<u8> {
Ok(1)
}

fn other() -> super::AppResult<u8, &'static str> {
Ok(2)
}
let default_result: super::AppResult<u8> = Ok(1);
let custom_result: super::AppResult<u8, &'static str> = Ok(2);

assert_eq!(app().unwrap(), 1);
assert_eq!(other().unwrap(), 2);
assert_eq!(default_result.unwrap(), 1);
assert_eq!(custom_result.unwrap(), 2);
}
58 changes: 47 additions & 11 deletions src/convert/tonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
//! ```rust,ignore
//! use masterror::{AppError, AppErrorKind};
//!
//! let status = tonic::Status::try_from(AppError::not_found("missing"))?;
//! let status = tonic::Status::from(AppError::not_found("missing"));
//! assert_eq!(status.code(), tonic::Code::NotFound);
//! ```

use std::{borrow::Cow, convert::Infallible};
use std::{borrow::Cow, fmt};

use tonic::{
Code, Status,
Expand All @@ -32,11 +32,49 @@ use crate::{
mapping_for_code
};

/// Error returned when converting [`Error`] into [`Status`] fails.
///
/// This type is never constructed in practice because the conversion is
/// guaranteed to succeed. It exists solely to preserve the `TryFrom` API in
/// addition to the infallible [`From`] conversion.
///
/// # Examples
/// ```rust,ignore
/// use masterror::{AppError, StatusConversionError};
/// use tonic::{Code, Status};
///
/// fn convert() -> Result<Status, StatusConversionError> {
/// Status::try_from(AppError::not_found("missing"))
/// }
///
/// # fn main() -> Result<(), StatusConversionError> {
/// let status = convert()?;
/// assert_eq!(status.code(), Code::NotFound);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StatusConversionError;

impl fmt::Display for StatusConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("conversion to tonic::Status cannot fail")
}
}

impl std::error::Error for StatusConversionError {}

impl From<Error> for Status {
fn from(error: Error) -> Self {
status_from_error(&error)
}
}

impl TryFrom<Error> for Status {
type Error = Infallible;
type Error = StatusConversionError;

fn try_from(error: Error) -> Result<Self, Self::Error> {
Ok(status_from_error(&error))
Ok(Status::from(error))
Comment on lines 73 to +77

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Avoid breaking TryFrom error type in patch release

The new TryFrom<Error> for Status now exposes type Error = StatusConversionError instead of the previous Infallible. Even though the conversion still never fails, changing this associated type is a breaking public API change: existing callers that return Result<Status, Infallible> or use ? on Status::try_from(err) will stop compiling when they upgrade from 0.20.4 to 0.20.5. Clippy’s infallible-conversion lint can be satisfied by adding impl From<Error> for Status while leaving the TryFrom error type as Infallible, which preserves backward compatibility.

Useful? React with 👍 / 👎.

}
}

Expand All @@ -59,10 +97,10 @@ fn status_from_error(error: &Error) -> Status {
if let Some(advice) = error.retry {
insert_retry(&mut meta, advice);
}
if let Some(challenge) = error.www_authenticate.as_deref() {
if is_ascii_metadata_value(challenge) {
insert_ascii(&mut meta, "www-authenticate", challenge);
}
if let Some(challenge) = error.www_authenticate.as_deref()
&& is_ascii_metadata_value(challenge)
{
insert_ascii(&mut meta, "www-authenticate", challenge);
}

if !matches!(error.edit_policy, MessageEditPolicy::Redact) {
Expand Down Expand Up @@ -123,9 +161,7 @@ fn metadata_value_to_ascii(value: &FieldValue) -> Option<Cow<'_, str>> {
}
FieldValue::I64(value) => Some(Cow::Owned(value.to_string())),
FieldValue::U64(value) => Some(Cow::Owned(value.to_string())),
FieldValue::Bool(value) => Some(Cow::Owned(
if *value { "true" } else { "false" }.to_string()
)),
FieldValue::Bool(value) => Some(Cow::Borrowed(if *value { "true" } else { "false" })),
FieldValue::Uuid(value) => Some(Cow::Owned(value.to_string()))
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,7 @@ pub use response::{
}
};
pub use result_ext::ResultExt;

#[cfg(feature = "tonic")]
#[cfg_attr(docsrs, doc(cfg(feature = "tonic")))]
pub use crate::convert::tonic::StatusConversionError;
Loading