Skip to main content

kmp_application/
application_error.rs

1use std::error::Error;
2use std::fmt;
3
4use kmp_domain::{DomainError, PortError};
5
6#[derive(Debug)]
7pub enum ApplicationError {
8    Domain(DomainError),
9    Ports(PortError),
10    /// Optimistic concurrency rejected this attempt before it committed.
11    /// Re-reading current state and replaying the same logical command with
12    /// the same idempotency key is safe.
13    RetryableConflict(String),
14    NotFound(String),
15    Validation(String),
16}
17
18impl fmt::Display for ApplicationError {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Self::Domain(error) => error.fmt(f),
22            Self::Ports(error) => error.fmt(f),
23            Self::RetryableConflict(message) => f.write_str(message),
24            Self::NotFound(message) => f.write_str(message),
25            Self::Validation(message) => f.write_str(message),
26        }
27    }
28}
29
30impl Error for ApplicationError {}
31
32impl From<DomainError> for ApplicationError {
33    fn from(value: DomainError) -> Self {
34        Self::Domain(value)
35    }
36}
37
38impl From<PortError> for ApplicationError {
39    fn from(value: PortError) -> Self {
40        Self::Ports(value)
41    }
42}