Skip to main content

cala_ledger/account_set/
error.rs

1use thiserror::Error;
2
3use super::repo::{
4    AccountSetColumn, AccountSetCreateError, AccountSetFindError, AccountSetModifyError,
5    AccountSetQueryError,
6};
7use crate::primitives::{AccountId, AccountSetId};
8
9#[derive(Error, Debug)]
10pub enum AccountSetError {
11    #[error("AccountSetError - Sqlx: {0}")]
12    Sqlx(sqlx::Error),
13    #[error("AccountSetError - Create: {0}")]
14    Create(AccountSetCreateError),
15    #[error("AccountSetError - Modify: {0}")]
16    Modify(#[from] AccountSetModifyError),
17    #[error("AccountSetError - Find: {0}")]
18    Find(AccountSetFindError),
19    #[error("AccountSetError - Query: {0}")]
20    Query(#[from] AccountSetQueryError),
21    #[error("AccountSetError - AccountError: {0}")]
22    AccountError(#[from] crate::account::error::AccountError),
23    #[error("AccountSetError - BalanceError: {0}")]
24    BalanceError(#[from] crate::balance::error::BalanceError),
25    #[error("AccountSetError - EntryError: {0}")]
26    EntryError(#[from] crate::entry::error::EntryError),
27    #[error("AccountSetError - NotFound: id '{0}' not found")]
28    CouldNotFindById(AccountSetId),
29    #[error("AccountSetError - NotFound: external id '{0}' not found")]
30    CouldNotFindByExternalId(String),
31    #[error("AccountSetError - external_id '{0}' already exists")]
32    ExternalIdAlreadyExists(String),
33    #[error("AccountSetError - JournalIdMismatch")]
34    JournalIdMismatch,
35    #[error("AccountSetError - Member already added to account set")]
36    MemberAlreadyAdded,
37    #[error(
38        "AccountSetError - Cannot add or remove member '{member_id}' to/from \
39         account set '{account_set_id}': member already has balance history \
40         in this journal"
41    )]
42    MemberHasBalanceHistory {
43        account_set_id: AccountSetId,
44        member_id: AccountId,
45    },
46    #[error(
47        "AccountSetError - Cannot add account set '{member_account_set_id}' as a member of \
48         account set '{account_set_id}': the member is already an ancestor of the set, \
49         so the membership would create a cycle"
50    )]
51    MembershipCycleDetected {
52        account_set_id: AccountSetId,
53        member_account_set_id: AccountSetId,
54    },
55    #[error(
56        "AccountSetError - Cannot add account set '{member_account_set_id}' as a member of \
57         account set '{account_set_id}': the resulting membership chain would be {depth} \
58         levels deep, exceeding the maximum of {max}"
59    )]
60    MembershipDepthExceeded {
61        account_set_id: AccountSetId,
62        member_account_set_id: AccountSetId,
63        depth: i32,
64        max: i32,
65    },
66}
67
68impl From<AccountSetFindError> for AccountSetError {
69    fn from(error: AccountSetFindError) -> Self {
70        match error {
71            AccountSetFindError::NotFound {
72                column: Some(AccountSetColumn::Id),
73                value,
74                ..
75            } => Self::CouldNotFindById(value.parse().expect("invalid uuid")),
76            AccountSetFindError::NotFound {
77                column: Some(AccountSetColumn::ExternalId),
78                value,
79                ..
80            } => Self::CouldNotFindByExternalId(value),
81            other => Self::Find(other),
82        }
83    }
84}
85
86impl From<AccountSetCreateError> for AccountSetError {
87    fn from(error: AccountSetCreateError) -> Self {
88        match error {
89            AccountSetCreateError::ConstraintViolation {
90                column: Some(AccountSetColumn::ExternalId),
91                value,
92                ..
93            } => Self::ExternalIdAlreadyExists(value.unwrap_or_default()),
94            other => Self::Create(other),
95        }
96    }
97}
98
99impl From<sqlx::Error> for AccountSetError {
100    fn from(error: sqlx::Error) -> Self {
101        if let Some(err) = error.as_database_error() {
102            if let Some(constraint) = err.constraint() {
103                if constraint
104                    .contains("cala_account_set_member_accou_account_set_id_member_account_key")
105                    || constraint
106                        .contains("cala_account_set_member_accou_account_set_id_member_accoun_key1")
107                {
108                    return Self::MemberAlreadyAdded;
109                }
110            }
111        }
112        Self::Sqlx(error)
113    }
114}