cala_ledger/transaction/
error.rs

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