User error

The Aggregate trait can return an AggregateError from its handle method. This error enum returns a variety of possible errors from optimistic locking violations to database connection errors.

The AggregateError::UserError(T) branch of the enum indicates that some rule of the business logic was violated, this information will usually be returned to the user as an error message. For example, an attempt to withdraw more money from a bank account than the current balance would return this error and the user would be informed that the balance was not sufficient for this transaction.

The payload of this user error is configurable. A simple UserErrorPayload is provided with the framework but here let's build our own, even simpler, error.


#![allow(unused)]
fn main() {
#[derive(Debug)]
pub struct BankAccountError(String);
}

This error should implement Display and Error as well. Additionally, implementing the From<&str> trait will simplify the business logic that we'll be writing in the next sections.


#![allow(unused)]
fn main() {
impl Display for BankAccountError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f,"{}",self.0)
    }
}

impl std::error::Error for BankAccountError {}

impl From<&str> for BankAccountError {
    fn from(message: &str) -> Self {
        BankAccountError(message.to_string())
    }
}
}