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.11.0] - 2025-10-26

### Changed
- Updated `AppError::database` to accept `Option<Cow<'static, str>>`, allowing
bare `None` calls without type annotations, and added the helper
`AppError::database_with_message` for the common message-bearing path.

### Documentation
- Refreshed the `AppError::database` docs to illustrate the new constructor
behavior and helper usage.

### Tests
- Expanded database constructor tests to cover both the helper and bare `None`
scenario.

## [0.10.9] - 2025-10-26

### 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.9"
version = "0.11.0"
rust-version = "1.90"
edition = "2024"
license = "MIT OR Apache-2.0"
Expand Down
15 changes: 8 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.9", default-features = false }
masterror = { version = "0.11.0", default-features = false }
# or with features:
# masterror = { version = "0.10.9", features = [
# masterror = { version = "0.11.0", features = [
# "axum", "actix", "openapi", "serde_json",
# "sqlx", "sqlx-migrate", "reqwest", "redis",
# "validator", "config", "tokio", "multipart",
Expand Down Expand Up @@ -66,10 +66,11 @@ masterror = { version = "0.10.9", default-features = false }
~~~toml
[dependencies]
# lean core
masterror = { version = "0.10.9", default-features = false }
masterror = { version = "0.11.0", default-features = false }

# with Axum/Actix + JSON + integrations
# masterror = { version = "0.10.9", features = [
# masterror = { version = "0.11.0", features = [

# "axum", "actix", "openapi", "serde_json",
# "sqlx", "sqlx-migrate", "reqwest", "redis",
# "validator", "config", "tokio", "multipart",
Expand Down Expand Up @@ -623,13 +624,13 @@ assert_eq!(resp.status, 401);
Minimal core:

~~~toml
masterror = { version = "0.10.9", default-features = false }
masterror = { version = "0.11.0", default-features = false }
~~~

API (Axum + JSON + deps):

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

~~~toml
masterror = { version = "0.10.9", features = [
masterror = { version = "0.11.0", features = [
"actix", "serde_json", "openapi",
"sqlx", "reqwest", "redis", "validator", "config", "tokio"
] }
Expand Down
31 changes: 27 additions & 4 deletions src/app_error/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,39 @@ impl AppError {
}
/// Build a `Database` error with an optional message.
///
/// Accepts `Option` to avoid gratuitous `.map(|...| ...)` at call sites
/// when you may or may not have a safe-to-print string at hand.
pub fn database(msg: Option<impl Into<Cow<'static, str>>>) -> Self {
/// This constructor accepts a pre-built [`Cow`] so callers that already
/// manage ownership can pass either borrowed or owned strings. When you
/// have plain string data, prefer [`AppError::database_with_message`].
///
/// ```rust
/// use masterror::AppError;
///
/// let err = AppError::database(None);
/// assert!(err.message.is_none());
/// ```
pub fn database(msg: Option<Cow<'static, str>>) -> Self {
Self {
kind: AppErrorKind::Database,
message: msg.map(Into::into),
message: msg,
retry: None,
www_authenticate: None
}
}

/// Build a `Database` error with a message.
///
/// Convenience wrapper around [`AppError::database`] for the common case
/// where you start from a plain string-like value.
///
/// ```rust
/// use masterror::AppError;
///
/// let err = AppError::database_with_message("db down");
/// assert_eq!(err.message.as_deref(), Some("db down"));
/// ```
pub fn database_with_message(msg: impl Into<Cow<'static, str>>) -> Self {
Self::database(Some(msg.into()))
}
/// Build a `Config` error.
pub fn config(msg: impl Into<Cow<'static, str>>) -> Self {
Self::with(AppErrorKind::Config, msg)
Expand Down
9 changes: 7 additions & 2 deletions src/app_error/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use super::{AppResult, core::AppError};
use crate::AppErrorKind;

Expand Down Expand Up @@ -108,10 +110,13 @@ fn constructors_match_kinds() {

#[test]
fn database_accepts_optional_message() {
let with_msg = AppError::database(Some("db down"));
let with_msg = AppError::database_with_message("db down");
assert_err_with_msg(with_msg, AppErrorKind::Database, "db down");

let without = AppError::database(None::<&str>);
let via_option = AppError::database(Some(Cow::Borrowed("db down")));
assert_err_with_msg(via_option, AppErrorKind::Database, "db down");

let without = AppError::database(None);
assert_err_bare(without, AppErrorKind::Database);
}

Expand Down
4 changes: 2 additions & 2 deletions src/convert/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl From<SqlxError> for AppError {
fn from(err: SqlxError) -> Self {
match err {
SqlxError::RowNotFound => AppError::not_found("Record not found"),
other => AppError::database(Some(other.to_string()))
other => AppError::database_with_message(other.to_string())
}
}
}
Expand All @@ -63,7 +63,7 @@ impl From<SqlxError> for AppError {
#[cfg_attr(docsrs, doc(cfg(feature = "sqlx-migrate")))]
impl From<MigrateError> for AppError {
fn from(err: MigrateError) -> Self {
AppError::database(Some(err.to_string()))
AppError::database_with_message(err.to_string())
}
}

Expand Down
Loading