pub struct DomainError { /* private fields */ }Expand description
Unified error type for the domain layer.
Modelled after std::io::Error, DomainError supports three
representations:
- a simple error that only carries a category;
- an error with a custom message;
- an error wrapping another error type, preserving the original concrete type so it can be downcast later.
§Construction
§Convenience constructors
use eventide_domain::error::DomainError;
// Invalid value
let err = DomainError::invalid_value("amount must be non-negative");
// Invalid state
let err = DomainError::invalid_state("order is closed and cannot be modified");
// Resource not found
let err = DomainError::not_found("user 123");
// Version conflict
let err = DomainError::conflict(1, 2);§Generic constructor
use eventide_domain::error::{DomainError, ErrorKind};
// Specify both category and message
let err = DomainError::new(ErrorKind::InvalidCommand, "insufficient stock");
// Custom error code
let err = DomainError::new(ErrorKind::NotFound, "user not found")
.with_code("USER_NOT_FOUND");§Wrapping a custom error
use eventide_domain::error::{DomainError, ErrorKind, ErrorCode};
use thiserror::Error;
#[derive(Debug, Error)]
#[error("custom error")]
struct MyError;
let err = DomainError::custom(ErrorKind::Internal, MyError);
// The original error can be retrieved.
assert!(err.downcast_ref::<MyError>().is_some());Implementations§
Source§impl DomainError
impl DomainError
Sourcepub const fn from_kind(kind: ErrorKind) -> Self
pub const fn from_kind(kind: ErrorKind) -> Self
Build a simple error from a category alone.
§Examples
use eventide_domain::error::{DomainError, ErrorKind};
let err = DomainError::from_kind(ErrorKind::NotFound);
assert_eq!(err.kind(), ErrorKind::NotFound);Sourcepub fn new(kind: ErrorKind, message: impl Into<Box<str>>) -> Self
pub fn new(kind: ErrorKind, message: impl Into<Box<str>>) -> Self
Build an error with a human-readable message.
§Examples
use eventide_domain::error::{DomainError, ErrorKind};
let err = DomainError::new(ErrorKind::InvalidValue, "amount must be positive");
assert_eq!(err.to_string(), "amount must be positive");Sourcepub fn custom<E>(kind: ErrorKind, error: E) -> Self
pub fn custom<E>(kind: ErrorKind, error: E) -> Self
Wrap an existing custom error.
The original error’s concrete type is preserved and can be
retrieved via DomainError::downcast_ref.
§Examples
use eventide_domain::error::{DomainError, ErrorKind};
use std::io;
let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let err = DomainError::custom(ErrorKind::Internal, io_err);
// Retrieve the original error.
let inner = err.downcast_ref::<io::Error>().unwrap();
assert_eq!(inner.kind(), io::ErrorKind::NotFound);Sourcepub fn with_code(self, code: &'static str) -> Self
pub fn with_code(self, code: &'static str) -> Self
Attach a custom error code.
§Examples
use eventide_domain::error::{DomainError, ErrorKind, ErrorCode};
let err = DomainError::not_found("user 123")
.with_code("USER_NOT_FOUND");
assert_eq!(err.code(), "USER_NOT_FOUND");Sourcepub fn invalid_value(msg: impl Into<Box<str>>) -> Self
pub fn invalid_value(msg: impl Into<Box<str>>) -> Self
Create an ErrorKind::InvalidValue error.
§Examples
use eventide_domain::error::{DomainError, ErrorKind, ErrorCode};
let err = DomainError::invalid_value("amount must be positive");
assert_eq!(err.kind(), ErrorKind::InvalidValue);
assert_eq!(err.http_status(), 400);Sourcepub fn invalid_state(msg: impl Into<Box<str>>) -> Self
pub fn invalid_state(msg: impl Into<Box<str>>) -> Self
Create an ErrorKind::InvalidState error.
§Examples
use eventide_domain::error::{DomainError, ErrorKind, ErrorCode};
let err = DomainError::invalid_state("order is closed");
assert_eq!(err.kind(), ErrorKind::InvalidState);
assert_eq!(err.http_status(), 422);Sourcepub fn invalid_command(msg: impl Into<Box<str>>) -> Self
pub fn invalid_command(msg: impl Into<Box<str>>) -> Self
Create an ErrorKind::InvalidCommand error.
§Examples
use eventide_domain::error::{DomainError, ErrorKind, ErrorCode};
let err = DomainError::invalid_command("insufficient stock");
assert_eq!(err.kind(), ErrorKind::InvalidCommand);
assert_eq!(err.http_status(), 400);Sourcepub fn not_found(msg: impl Into<Box<str>>) -> Self
pub fn not_found(msg: impl Into<Box<str>>) -> Self
Create an ErrorKind::NotFound error.
§Examples
use eventide_domain::error::{DomainError, ErrorKind, ErrorCode};
let err = DomainError::not_found("user 123");
assert_eq!(err.kind(), ErrorKind::NotFound);
assert_eq!(err.http_status(), 404);Sourcepub fn conflict(expected: impl Display, actual: impl Display) -> Self
pub fn conflict(expected: impl Display, actual: impl Display) -> Self
Create an ErrorKind::Conflict error.
§Examples
use eventide_domain::error::DomainError;
// Accepts any `Display` types.
let err = DomainError::conflict(1_u64, 2_u64);
let err = DomainError::conflict(1_usize, 2_usize);
let err = DomainError::conflict("v1", "v2");Sourcepub fn internal(msg: impl Into<Box<str>>) -> Self
pub fn internal(msg: impl Into<Box<str>>) -> Self
Create an ErrorKind::Internal error.
§Examples
use eventide_domain::error::{DomainError, ErrorKind, ErrorCode};
let err = DomainError::internal("database connection failed");
assert_eq!(err.kind(), ErrorKind::Internal);
assert_eq!(err.http_status(), 500);Sourcepub fn upcast_failed(
event_type: impl Into<Box<str>>,
from_version: usize,
stage: Option<&'static str>,
reason: impl Into<Box<str>>,
) -> Self
pub fn upcast_failed( event_type: impl Into<Box<str>>, from_version: usize, stage: Option<&'static str>, reason: impl Into<Box<str>>, ) -> Self
Create an “event upcast failed” error.
Sourcepub fn type_mismatch(
expected: impl Into<Box<str>>,
found: impl Into<Box<str>>,
) -> Self
pub fn type_mismatch( expected: impl Into<Box<str>>, found: impl Into<Box<str>>, ) -> Self
Create a “type mismatch” error.
Sourcepub fn downcast_ref<E: StdError + 'static>(&self) -> Option<&E>
pub fn downcast_ref<E: StdError + 'static>(&self) -> Option<&E>
Try to downcast this error into a concrete type.
Only succeeds when the error was constructed via
DomainError::custom.
Sourcepub fn get_ref(&self) -> Option<&(dyn StdError + Send + Sync + 'static)>
pub fn get_ref(&self) -> Option<&(dyn StdError + Send + Sync + 'static)>
Return a reference to the wrapped inner error, if any.
Sourcepub fn static_code(&self) -> &'static str
pub fn static_code(&self) -> &'static str
Return the error code as a &'static str.
Unlike ErrorCode::code this method returns &'static str,
which is convenient when the code needs to be stored elsewhere
without lifetime gymnastics.
Sourcepub fn matches(&self, kind: ErrorKind, code: &str) -> bool
pub fn matches(&self, kind: ErrorKind, code: &str) -> bool
Check whether this error matches the given category and code.
Useful in tests and conditional logic.
§Examples
use eventide_domain::error::{DomainError, ErrorKind};
let err = DomainError::not_found("user").with_code("USER_NOT_FOUND");
assert!(err.matches(ErrorKind::NotFound, "USER_NOT_FOUND"));
assert!(!err.matches(ErrorKind::NotFound, "NOT_FOUND"));
assert!(!err.matches(ErrorKind::Internal, "USER_NOT_FOUND"));Trait Implementations§
Source§impl Debug for DomainError
impl Debug for DomainError
Source§impl Display for DomainError
impl Display for DomainError
Source§impl Error for DomainError
impl Error for DomainError
Source§fn source(&self) -> Option<&(dyn StdError + 'static)>
fn source(&self) -> Option<&(dyn StdError + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl ErrorCode for DomainError
impl ErrorCode for DomainError
Source§fn code(&self) -> &str
fn code(&self) -> &str
ErrorKind::default_code).Source§fn http_status(&self) -> u16
fn http_status(&self) -> u16
ErrorKind::http_status).Source§fn is_retryable(&self) -> bool
fn is_retryable(&self) -> bool
ErrorKind::is_retryable).Source§impl From<Error> for DomainError
impl From<Error> for DomainError
Source§impl From<Error> for DomainError
impl From<Error> for DomainError
Source§impl From<Error> for DomainError
impl From<Error> for DomainError
Source§impl From<Error> for DomainError
Available on crate feature infra-sqlx only.
impl From<Error> for DomainError
infra-sqlx only.Source§impl From<ErrorKind> for DomainError
impl From<ErrorKind> for DomainError
Source§impl From<ParseBoolError> for DomainError
impl From<ParseBoolError> for DomainError
Source§fn from(err: ParseBoolError) -> Self
fn from(err: ParseBoolError) -> Self
Source§impl From<ParseError> for DomainError
impl From<ParseError> for DomainError
Source§fn from(err: ParseError) -> Self
fn from(err: ParseError) -> Self
Source§impl From<ParseFloatError> for DomainError
impl From<ParseFloatError> for DomainError
Source§fn from(err: ParseFloatError) -> Self
fn from(err: ParseFloatError) -> Self
Source§impl From<ParseIntError> for DomainError
impl From<ParseIntError> for DomainError
Source§fn from(err: ParseIntError) -> Self
fn from(err: ParseIntError) -> Self
Auto Trait Implementations§
impl Freeze for DomainError
impl !RefUnwindSafe for DomainError
impl Send for DomainError
impl Sync for DomainError
impl Unpin for DomainError
impl UnsafeUnpin for DomainError
impl !UnwindSafe for DomainError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more