cala_ledger/account/
error.rs

1use thiserror::Error;
2
3use crate::primitives::AccountId;
4
5#[derive(Error, Debug)]
6pub enum AccountError {
7    #[error("AccountError - Sqlx: {0}")]
8    Sqlx(sqlx::Error),
9    #[error("AccountError - EsEntityError: {0}")]
10    EsEntityError(es_entity::EsEntityError),
11    #[error("AccountError - CursorDestructureError: {0}")]
12    CursorDestructureError(#[from] es_entity::CursorDestructureError),
13    #[error("AccountError - NotFound: id '{0}' not found")]
14    CouldNotFindById(AccountId),
15    #[error("AccountError - NotFound: external id '{0}' not found")]
16    CouldNotFindByExternalId(String),
17    #[error("AccountError - NotFound: code '{0}' not found")]
18    CouldNotFindByCode(String),
19    #[error("AccountError - external_id already exists")]
20    ExternalIdAlreadyExists,
21    #[error("AccountError - code already exists")]
22    CodeAlreadyExists,
23}
24
25impl From<sqlx::Error> for AccountError {
26    fn from(error: sqlx::Error) -> Self {
27        if let Some(err) = error.as_database_error() {
28            if let Some(constraint) = err.constraint() {
29                if constraint.contains("external_id") {
30                    return Self::ExternalIdAlreadyExists;
31                } else if constraint.contains("code") {
32                    return Self::CodeAlreadyExists;
33                }
34            }
35        }
36        Self::Sqlx(error)
37    }
38}
39
40es_entity::from_es_entity_error!(AccountError);