cala_ledger/account_set/
error.rs

1use thiserror::Error;
2
3use crate::primitives::AccountSetId;
4
5#[derive(Error, Debug)]
6pub enum AccountSetError {
7    #[error("AccountSetError - Sqlx: {0}")]
8    Sqlx(sqlx::Error),
9    #[error("AccountSetError - EsEntityError: {0}")]
10    EsEntityError(es_entity::EsEntityError),
11    #[error("AccountSetError - CursorDestructureError: {0}")]
12    CursorDestructureError(#[from] es_entity::CursorDestructureError),
13    #[error("AccountSetError - AccountError: {0}")]
14    AccountError(#[from] crate::account::error::AccountError),
15    #[error("AccountSetError - BalanceError: {0}")]
16    BalanceError(#[from] crate::balance::error::BalanceError),
17    #[error("AccountSetError - EntryError: {0}")]
18    EntryError(#[from] crate::entry::error::EntryError),
19    #[error("AccountSetError - NotFound: id '{0}' not found")]
20    CouldNotFindById(AccountSetId),
21    #[error("AccountSetError - NotFound: external id '{0}' not found")]
22    CouldNotFindByExternalId(String),
23    #[error("AccountSetError - external_id already exists")]
24    ExternalIdAlreadyExists,
25    #[error("AccountSetError - JournalIdMismatch")]
26    JournalIdMismatch,
27    #[error("AccountSetError - Member already added to account set")]
28    MemberAlreadyAdded,
29}
30
31es_entity::from_es_entity_error!(AccountSetError);
32
33impl From<sqlx::Error> for AccountSetError {
34    fn from(error: sqlx::Error) -> Self {
35        if let Some(err) = error.as_database_error() {
36            if let Some(constraint) = err.constraint() {
37                if constraint.contains("external_id") {
38                    return Self::ExternalIdAlreadyExists;
39                } else if constraint
40                    .contains("cala_account_set_member_accou_account_set_id_member_account_key")
41                    || constraint
42                        .contains("cala_account_set_member_accou_account_set_id_member_accoun_key1")
43                {
44                    return Self::MemberAlreadyAdded;
45                }
46            }
47        }
48        Self::Sqlx(error)
49    }
50}