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 recalculate account set '{account_set_id}': \
48         only eventually-consistent sets support recalculation"
49    )]
50    CannotRecalculateNonEcSet { account_set_id: AccountSetId },
51}
52
53impl From<AccountSetFindError> for AccountSetError {
54    fn from(error: AccountSetFindError) -> Self {
55        match error {
56            AccountSetFindError::NotFound {
57                column: Some(AccountSetColumn::Id),
58                value,
59                ..
60            } => Self::CouldNotFindById(value.parse().expect("invalid uuid")),
61            AccountSetFindError::NotFound {
62                column: Some(AccountSetColumn::ExternalId),
63                value,
64                ..
65            } => Self::CouldNotFindByExternalId(value),
66            other => Self::Find(other),
67        }
68    }
69}
70
71impl From<AccountSetCreateError> for AccountSetError {
72    fn from(error: AccountSetCreateError) -> Self {
73        match error {
74            AccountSetCreateError::ConstraintViolation {
75                column: Some(AccountSetColumn::ExternalId),
76                value,
77                ..
78            } => Self::ExternalIdAlreadyExists(value.unwrap_or_default()),
79            other => Self::Create(other),
80        }
81    }
82}
83
84impl From<sqlx::Error> for AccountSetError {
85    fn from(error: sqlx::Error) -> Self {
86        if let Some(err) = error.as_database_error() {
87            if let Some(constraint) = err.constraint() {
88                if constraint
89                    .contains("cala_account_set_member_accou_account_set_id_member_account_key")
90                    || constraint
91                        .contains("cala_account_set_member_accou_account_set_id_member_accoun_key1")
92                {
93                    return Self::MemberAlreadyAdded;
94                }
95            }
96        }
97        Self::Sqlx(error)
98    }
99}