cqrs_es/
error.rs

1use std::error;
2
3/// The base error for the framework.
4#[derive(Debug, thiserror::Error)]
5pub enum AggregateError<T: error::Error> {
6    /// This is the error returned when a user violates a business rule. The payload within
7    /// `AggregateError::UserError` should be used to pass information to inform the user of
8    /// the nature of problem.
9    ///
10    /// The `UserErrorPayload` struct has been provided as a reference implementation for this
11    /// purpose.
12    ///
13    /// ### Handling
14    /// In a Restful application this should translate to a 400 response status.
15    #[error("{0}")]
16    UserError(T),
17    /// A command has been rejected due to a conflict with another command on the same aggregate
18    /// instance. This is handled by optimistic locking in systems backed by an RDBMS.
19    ///
20    /// ### Handling
21    /// In a Restful application this usually translates to a 503 or 429 response status, often with
22    /// a [Retry-After response header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After)
23    /// indicating that the user should try again.
24    #[error("aggregate conflict")]
25    AggregateConflict,
26    /// A error occurred while attempting to read or write from a database.
27    #[error("{0}")]
28    DatabaseConnectionError(Box<dyn error::Error + Send + Sync + 'static>),
29    /// A deserialization error occurred due to invalid JSON.
30    #[error("{0}")]
31    DeserializationError(Box<dyn error::Error + Send + Sync + 'static>),
32    /// A technical error was encountered that prevented the command from being applied to the
33    /// aggregate. In general the accompanying message should be logged for investigation rather
34    /// than returned to the user.
35    #[error("{0}")]
36    UnexpectedError(Box<dyn error::Error + Send + Sync + 'static>),
37}