Skip to main content

cala_ledger/journal/
error.rs

1use thiserror::Error;
2
3use super::repo::{
4    JournalColumn, JournalCreateError, JournalFindError, JournalModifyError, JournalQueryError,
5};
6
7#[derive(Error, Debug)]
8pub enum JournalError {
9    #[error("JournalError - Sqlx: {0}")]
10    Sqlx(#[from] sqlx::Error),
11    #[error("JournalError - Create: {0}")]
12    Create(JournalCreateError),
13    #[error("JournalError - Modify: {0}")]
14    Modify(#[from] JournalModifyError),
15    #[error("JournalError - Find: {0}")]
16    Find(#[from] JournalFindError),
17    #[error("JournalError - Query: {0}")]
18    Query(#[from] JournalQueryError),
19    #[error("JournalError - code '{0}' already exists")]
20    CodeAlreadyExists(String),
21}
22
23impl From<JournalCreateError> for JournalError {
24    fn from(error: JournalCreateError) -> Self {
25        match error {
26            JournalCreateError::ConstraintViolation {
27                column: Some(JournalColumn::Code),
28                value,
29                ..
30            } => Self::CodeAlreadyExists(value.unwrap_or_default()),
31            other => Self::Create(other),
32        }
33    }
34}