Skip to main content

world_tax/
errors.rs

1//! Error types and handling.
2//!
3//! This module defines the various error types used throughout the tax calculation
4//! system. These errors are used to represent different failure scenarios that can
5//! occur during input validation, database operations, and processing of tax calculations.
6//!
7//! The errors are categorized into three main types:
8//!
9//! - `InputValidationError`: Errors related to invalid input data, such as incorrect
10//!   country or region codes.
11//! - `DatabaseError`: Errors that occur during database operations, such as missing
12//!   trade agreements or tax rates.
13//! - `ProcessingError`: Errors that occur during the processing of tax calculations,
14//!   such as invalid amounts or errors propagated from other error types.
15
16use serde::Serialize;
17use thiserror::Error;
18
19#[derive(Debug, Error, Serialize)]
20pub enum InputValidationError {
21    #[error("Invalid country code: {0}")]
22    InvalidCountryCode(String),
23    #[error("Invalid region code: {0}")]
24    InvalidRegionCode(String),
25    #[error("Unexpected region code: {0} - Country has no regions.")]
26    UnexpectedRegionCode(String),
27}
28
29#[derive(Debug, Error, Serialize)]
30pub enum DatabaseError {
31    #[error("Trade agreement not found: {0}")]
32    TradeAgreementNotFound(String),
33    #[error("Country not found: {0}")]
34    CountryNotFound(String),
35    #[error("Region not found: {0}")]
36    RegionNotFound(String),
37    #[error("VAT rate not found: {0}")]
38    VatRateNotFound(String),
39}
40
41#[derive(Debug, Error, Serialize)]
42pub enum ProcessingError {
43    #[error("Invalid input: {0}")]
44    InputValidationError(InputValidationError),
45    #[error("Database error: {0}")]
46    DatabaseError(DatabaseError),
47    #[error("Invalid amount")]
48    InvalidAmount,
49}
50
51impl From<InputValidationError> for ProcessingError {
52    fn from(err: InputValidationError) -> Self {
53        ProcessingError::InputValidationError(err)
54    }
55}
56
57impl From<DatabaseError> for ProcessingError {
58    fn from(err: DatabaseError) -> Self {
59        ProcessingError::DatabaseError(err)
60    }
61}