cala_ledger/transaction/
error.rs1use thiserror::Error;
2
3use super::repo::{
4 TransactionColumn, TransactionCreateError, TransactionFindError, TransactionModifyError,
5 TransactionQueryError,
6};
7use cala_types::primitives::TransactionId;
8
9#[derive(Error, Debug)]
10pub enum TransactionError {
11 #[error("TransactionError - Sqlx: {0}")]
12 Sqlx(#[from] sqlx::Error),
13 #[error("TransactionError - Create: {0}")]
14 Create(TransactionCreateError),
15 #[error("TransactionError - Modify: {0}")]
16 Modify(#[from] TransactionModifyError),
17 #[error("TransactionError - Find: {0}")]
18 Find(TransactionFindError),
19 #[error("TransactionError - Query: {0}")]
20 Query(#[from] TransactionQueryError),
21 #[error("TransactionError - NotFound: id '{0}' not found")]
22 CouldNotFindById(TransactionId),
23 #[error("TransactionError - NotFound: external id '{0}' not found")]
24 CouldNotFindByExternalId(String),
25 #[error("TransactionError - DuplicateExternalId: external_id '{0}' already exists")]
26 DuplicateExternalId(String),
27 #[error("TransactionError - DuplicateId: id '{0}' already exists")]
28 DuplicateId(String),
29}
30
31impl TransactionError {
32 pub fn was_not_found(&self) -> bool {
33 matches!(
34 self,
35 Self::CouldNotFindById(_) | Self::CouldNotFindByExternalId(_)
36 )
37 }
38}
39
40impl From<TransactionFindError> for TransactionError {
41 fn from(error: TransactionFindError) -> Self {
42 match error {
43 TransactionFindError::NotFound {
44 column: Some(TransactionColumn::Id),
45 value,
46 ..
47 } => Self::CouldNotFindById(value.parse().expect("invalid uuid")),
48 TransactionFindError::NotFound {
49 column: Some(TransactionColumn::ExternalId),
50 value,
51 ..
52 } => Self::CouldNotFindByExternalId(value),
53 other => Self::Find(other),
54 }
55 }
56}
57
58impl From<TransactionCreateError> for TransactionError {
59 fn from(error: TransactionCreateError) -> Self {
60 match error {
61 TransactionCreateError::ConstraintViolation {
62 column: Some(TransactionColumn::ExternalId),
63 value,
64 ..
65 } => Self::DuplicateExternalId(value.unwrap_or_default()),
66 TransactionCreateError::ConstraintViolation {
67 column: Some(TransactionColumn::Id),
68 value,
69 ..
70 } => Self::DuplicateId(value.unwrap_or_default()),
71 other => Self::Create(other),
72 }
73 }
74}