Skip to main content

cala_ledger/account/
error.rs

1use thiserror::Error;
2
3use super::repo::{
4    AccountColumn, AccountCreateError, AccountFindError, AccountModifyError, AccountQueryError,
5};
6use crate::primitives::AccountId;
7
8#[derive(Error, Debug)]
9pub enum AccountError {
10    #[error("AccountError - Sqlx: {0}")]
11    Sqlx(#[from] sqlx::Error),
12    #[error("AccountError - Create: {0}")]
13    Create(AccountCreateError),
14    #[error("AccountError - Modify: {0}")]
15    Modify(#[from] AccountModifyError),
16    #[error("AccountError - Find: {0}")]
17    Find(AccountFindError),
18    #[error("AccountError - Query: {0}")]
19    Query(#[from] AccountQueryError),
20    #[error("AccountError - NotFound: id '{0}' not found")]
21    CouldNotFindById(AccountId),
22    #[error("AccountError - NotFound: external id '{0}' not found")]
23    CouldNotFindByExternalId(String),
24    #[error("AccountError - NotFound: code '{0}' not found")]
25    CouldNotFindByCode(String),
26    #[error("AccountError - external_id '{0}' already exists")]
27    ExternalIdAlreadyExists(String),
28    #[error("AccountError - code '{0}' already exists")]
29    CodeAlreadyExists(String),
30    #[error("AccountError - cannot update accounts backing an AccountSet")]
31    CannotUpdateAccountSetAccounts,
32}
33
34impl From<AccountFindError> for AccountError {
35    fn from(error: AccountFindError) -> Self {
36        match error {
37            AccountFindError::NotFound {
38                column: Some(AccountColumn::Id),
39                value,
40                ..
41            } => Self::CouldNotFindById(value.parse().expect("invalid uuid")),
42            AccountFindError::NotFound {
43                column: Some(AccountColumn::ExternalId),
44                value,
45                ..
46            } => Self::CouldNotFindByExternalId(value),
47            AccountFindError::NotFound {
48                column: Some(AccountColumn::Code),
49                value,
50                ..
51            } => Self::CouldNotFindByCode(value),
52            other => Self::Find(other),
53        }
54    }
55}
56
57impl From<AccountCreateError> for AccountError {
58    fn from(error: AccountCreateError) -> Self {
59        match error {
60            AccountCreateError::ConstraintViolation {
61                column: Some(AccountColumn::ExternalId),
62                value,
63                ..
64            } => Self::ExternalIdAlreadyExists(value.unwrap_or_default()),
65            AccountCreateError::ConstraintViolation {
66                column: Some(AccountColumn::Code),
67                value,
68                ..
69            } => Self::CodeAlreadyExists(value.unwrap_or_default()),
70            other => Self::Create(other),
71        }
72    }
73}