Skip to main content

cala_ledger/entry/
error.rs

1use thiserror::Error;
2
3use super::repo::{
4    EntryConstraint, EntryCreateError, EntryFindError, EntryModifyError, EntryQueryError,
5};
6
7#[derive(Error, Debug)]
8pub enum EntryError {
9    #[error("EntryError - Sqlx: {0}")]
10    Sqlx(#[from] sqlx::Error),
11    #[error("EntryError - Create: {0}")]
12    Create(EntryCreateError),
13    #[error("EntryError - Modify: {0}")]
14    Modify(#[from] EntryModifyError),
15    #[error("EntryError - Find: {0}")]
16    Find(#[from] EntryFindError),
17    #[error("EntryError - Query: {0}")]
18    Query(#[from] EntryQueryError),
19    #[error(
20        "EntryError - EntryTargetsAccountSet: an entry may not be posted directly to an \
21         account-set backing account; an account set's balance is derived from its members"
22    )]
23    EntryTargetsAccountSet,
24}
25
26impl From<EntryCreateError> for EntryError {
27    fn from(error: EntryCreateError) -> Self {
28        if error.violated_constraint() == Some(EntryConstraint::AccountNotAccountSetFkey) {
29            Self::EntryTargetsAccountSet
30        } else {
31            Self::Create(error)
32        }
33    }
34}