Skip to main content

DomainError

Struct DomainError 

Source
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

Source

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);
Source

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");
Source

pub fn custom<E>(kind: ErrorKind, error: E) -> Self
where E: StdError + Send + Sync + 'static,

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);
Source

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");
Source

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);
Source

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);
Source

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);
Source

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);
Source

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");
Source

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);
Source

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.

Source

pub fn type_mismatch( expected: impl Into<Box<str>>, found: impl Into<Box<str>>, ) -> Self

Create a “type mismatch” error.

Source

pub fn event_bus(reason: impl Into<Box<str>>) -> Self

Create an “event bus” error.

Source

pub fn kind(&self) -> ErrorKind

Return the error category.

Source

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.

Source

pub fn get_ref(&self) -> Option<&(dyn StdError + Send + Sync + 'static)>

Return a reference to the wrapped inner error, if any.

Source

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.

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for DomainError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for DomainError

Source§

fn source(&self) -> Option<&(dyn StdError + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl ErrorCode for DomainError

Source§

fn kind(&self) -> ErrorKind

Return the error’s category.
Source§

fn code(&self) -> &str

Return the error code (defaults to ErrorKind::default_code).
Source§

fn http_status(&self) -> u16

Return the HTTP status code (defaults to ErrorKind::http_status).
Source§

fn is_retryable(&self) -> bool

Whether the error is retryable (defaults to ErrorKind::is_retryable).
Source§

impl From<Error> for DomainError

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for DomainError

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for DomainError

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for DomainError

Available on crate feature infra-sqlx only.
Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<ErrorKind> for DomainError

Source§

fn from(kind: ErrorKind) -> Self

Converts to this type from the input type.
Source§

impl From<ParseBoolError> for DomainError

Source§

fn from(err: ParseBoolError) -> Self

Converts to this type from the input type.
Source§

impl From<ParseError> for DomainError

Source§

fn from(err: ParseError) -> Self

Converts to this type from the input type.
Source§

impl From<ParseFloatError> for DomainError

Source§

fn from(err: ParseFloatError) -> Self

Converts to this type from the input type.
Source§

impl From<ParseIntError> for DomainError

Source§

fn from(err: ParseIntError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,