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, AccountSetId};
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    #[error("AccountError - initial account set '{0}' not found")]
33    InitialAccountSetNotFound(AccountSetId),
34}
35
36impl From<AccountFindError> for AccountError {
37    fn from(error: AccountFindError) -> Self {
38        match error {
39            AccountFindError::NotFound {
40                column: Some(AccountColumn::Id),
41                value,
42                ..
43            } => Self::CouldNotFindById(value.parse().expect("invalid uuid")),
44            AccountFindError::NotFound {
45                column: Some(AccountColumn::ExternalId),
46                value,
47                ..
48            } => Self::CouldNotFindByExternalId(value),
49            AccountFindError::NotFound {
50                column: Some(AccountColumn::Code),
51                value,
52                ..
53            } => Self::CouldNotFindByCode(value),
54            other => Self::Find(other),
55        }
56    }
57}
58
59impl From<AccountCreateError> for AccountError {
60    fn from(error: AccountCreateError) -> Self {
61        match error {
62            AccountCreateError::ConstraintViolation {
63                column: Some(AccountColumn::ExternalId),
64                value,
65                ..
66            } => Self::ExternalIdAlreadyExists(value.unwrap_or_default()),
67            AccountCreateError::ConstraintViolation {
68                column: Some(AccountColumn::Code),
69                value,
70                ..
71            } => Self::CodeAlreadyExists(value.unwrap_or_default()),
72            other => Self::Create(other),
73        }
74    }
75}