pub struct Error { /* private fields */ }Expand description
Rich application error preserving domain code, taxonomy and metadata.
Implementations§
Source§impl AppError
impl AppError
Sourcepub fn validation(msg: impl Into<Cow<'static, str>>) -> Self
pub fn validation(msg: impl Into<Cow<'static, str>>) -> Self
Build a Validation error.
Build an Unauthorized error.
Sourcepub fn bad_request(msg: impl Into<Cow<'static, str>>) -> Self
pub fn bad_request(msg: impl Into<Cow<'static, str>>) -> Self
Build a BadRequest error.
Sourcepub fn rate_limited(msg: impl Into<Cow<'static, str>>) -> Self
pub fn rate_limited(msg: impl Into<Cow<'static, str>>) -> Self
Build a RateLimited error.
Sourcepub fn telegram_auth(msg: impl Into<Cow<'static, str>>) -> Self
pub fn telegram_auth(msg: impl Into<Cow<'static, str>>) -> Self
Build a TelegramAuth error.
Sourcepub fn service(msg: impl Into<Cow<'static, str>>) -> Self
pub fn service(msg: impl Into<Cow<'static, str>>) -> Self
Build a Service error (generic server-side service failure).
Sourcepub fn database(msg: Option<Cow<'static, str>>) -> Self
pub fn database(msg: Option<Cow<'static, str>>) -> Self
Build a Database error with an optional message.
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.
use masterror::AppError;
let err = AppError::database(None);
assert!(err.message.is_none());Sourcepub fn database_with_message(msg: impl Into<Cow<'static, str>>) -> Self
pub fn database_with_message(msg: impl Into<Cow<'static, str>>) -> Self
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.
use masterror::AppError;
let err = AppError::database_with_message("db down");
assert_eq!(err.message.as_deref(), Some("db down"));Build a DependencyUnavailable error.
Backward-compatible alias; routes to DependencyUnavailable.
Sourcepub fn serialization(msg: impl Into<Cow<'static, str>>) -> Self
pub fn serialization(msg: impl Into<Cow<'static, str>>) -> Self
Build a Serialization error.
Sourcepub fn deserialization(msg: impl Into<Cow<'static, str>>) -> Self
pub fn deserialization(msg: impl Into<Cow<'static, str>>) -> Self
Build a Deserialization error.
Sourcepub fn external_api(msg: impl Into<Cow<'static, str>>) -> Self
pub fn external_api(msg: impl Into<Cow<'static, str>>) -> Self
Build an ExternalApi error.
Source§impl Error
impl Error
Sourcepub fn new(kind: AppErrorKind, msg: impl Into<Cow<'static, str>>) -> Self
pub fn new(kind: AppErrorKind, msg: impl Into<Cow<'static, str>>) -> Self
Create a new Error with a kind and message.
This is equivalent to Error::with, provided for API symmetry and to
keep doctests readable.
§Examples
use masterror::{AppError, AppErrorKind};
let err = AppError::new(AppErrorKind::BadRequest, "invalid payload");
assert!(err.message.is_some());Sourcepub fn with(kind: AppErrorKind, msg: impl Into<Cow<'static, str>>) -> Self
pub fn with(kind: AppErrorKind, msg: impl Into<Cow<'static, str>>) -> Self
Create an error with the given kind and message.
Prefer named helpers (e.g. Error::not_found) where it clarifies
intent.
Sourcepub fn bare(kind: AppErrorKind) -> Self
pub fn bare(kind: AppErrorKind) -> Self
Create a message-less error with the given kind.
Useful when the kind alone conveys sufficient information to the client.
Sourcepub fn with_retry_after_secs(self, secs: u64) -> Self
pub fn with_retry_after_secs(self, secs: u64) -> Self
Attach retry advice to the error.
When mapped to HTTP, this becomes the Retry-After header.
Sourcepub fn with_www_authenticate(self, value: impl Into<String>) -> Self
pub fn with_www_authenticate(self, value: impl Into<String>) -> Self
Attach a WWW-Authenticate challenge string.
Sourcepub fn with_field(self, field: Field) -> Self
pub fn with_field(self, field: Field) -> Self
Attach additional metadata to the error.
Sourcepub fn with_fields(self, fields: impl IntoIterator<Item = Field>) -> Self
pub fn with_fields(self, fields: impl IntoIterator<Item = Field>) -> Self
Extend metadata from an iterator of fields.
Sourcepub fn redact_field(self, name: &'static str, redaction: FieldRedaction) -> Self
pub fn redact_field(self, name: &'static str, redaction: FieldRedaction) -> Self
Override the redaction policy for a stored metadata field.
Sourcepub fn with_metadata(self, metadata: Metadata) -> Self
pub fn with_metadata(self, metadata: Metadata) -> Self
Replace metadata entirely.
Sourcepub fn redactable(self) -> Self
pub fn redactable(self) -> Self
Mark the message as redactable.
Sourcepub fn with_context(self, context: impl Into<ContextAttachment>) -> Self
pub fn with_context(self, context: impl Into<ContextAttachment>) -> Self
Attach upstream diagnostics using with_source or
an existing Arc.
This is the preferred alias for capturing upstream errors. It accepts
either an owned error implementing core::error::Error or a
shared Arc produced by other APIs, reusing the allocation when
possible.
§Examples
use masterror::AppError;
let err = AppError::service("downstream degraded")
.with_context(std::io::Error::new(std::io::ErrorKind::Other, "boom"));
assert!(err.source_ref().is_some());Sourcepub fn with_source(self, source: impl CoreError + Send + Sync + 'static) -> Self
pub fn with_source(self, source: impl CoreError + Send + Sync + 'static) -> Self
Attach a source error for diagnostics.
Prefer with_context when capturing upstream
diagnostics without additional Arc allocations.
Sourcepub fn with_source_arc(
self,
source: Arc<dyn CoreError + Send + Sync + 'static>,
) -> Self
pub fn with_source_arc( self, source: Arc<dyn CoreError + Send + Sync + 'static>, ) -> Self
Attach a shared source error without cloning the underlying Arc.
§Examples
use std::sync::Arc;
use masterror::{AppError, AppErrorKind};
let source = Arc::new(std::io::Error::new(std::io::ErrorKind::Other, "boom"));
let err = AppError::internal("boom").with_source_arc(source.clone());
assert!(err.source_ref().is_some());
assert_eq!(Arc::strong_count(&source), 2);Sourcepub fn with_backtrace(self, backtrace: Backtrace) -> Self
pub fn with_backtrace(self, backtrace: Backtrace) -> Self
Attach a captured backtrace.
Sourcepub fn with_details_text(self, details: impl Into<String>) -> Self
Available on non-crate feature serde_json only.
pub fn with_details_text(self, details: impl Into<String>) -> Self
serde_json only.Attach plain-text details for client payloads.
The text is omitted from responses when the error is
redactable.
§Examples
use masterror::{AppError, AppErrorKind};
let err = AppError::new(AppErrorKind::Internal, "boom").with_details_text("retry later");
assert!(err.details.is_some());Sourcepub fn backtrace(&self) -> Option<&Backtrace>
pub fn backtrace(&self) -> Option<&Backtrace>
Borrow the backtrace, capturing it lazily when the backtrace feature
is enabled.
Sourcepub fn source_ref(&self) -> Option<&(dyn CoreError + Send + Sync + 'static)>
pub fn source_ref(&self) -> Option<&(dyn CoreError + Send + Sync + 'static)>
Borrow the source if present.
Sourcepub fn render_message(&self) -> Cow<'_, str>
pub fn render_message(&self) -> Cow<'_, str>
Human-readable message or the kind fallback.
Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn CoreError + 'static)>
fn source(&self) -> Option<&(dyn CoreError + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl From<&Error> for ErrorResponse
impl From<&Error> for ErrorResponse
Source§impl From<Error> for AppError
Available on crate feature std only.Map std::io::Error to an internal application error.
impl From<Error> for AppError
std only.Map std::io::Error to an internal application error.
Rationale: I/O failures are infrastructure-level and should not leak
driver-specific details to clients. The message is preserved for
observability, but the public-facing kind is always Internal.
use std::io::{self, ErrorKind};
use masterror::{AppError, AppErrorKind};
let io_err = io::Error::from(ErrorKind::Other);
let app_err: AppError = io_err.into();
assert!(matches!(app_err.kind, AppErrorKind::Internal));Source§impl From<Error> for ErrorResponse
impl From<Error> for ErrorResponse
Source§impl From<String> for AppError
Map a plain String to a client error (BadRequest).
impl From<String> for AppError
Map a plain String to a client error (BadRequest).
Handy for quick validation paths without the validator feature.
Prefer structured validation for complex DTOs, but this covers simple cases.
use masterror::{AppError, AppErrorKind, AppResult};
fn check(name: &str) -> AppResult<()> {
if name.is_empty() {
return Err(String::from("name must not be empty").into());
}
Ok(())
}
let err = check("").unwrap_err();
assert!(matches!(err.kind, AppErrorKind::BadRequest));