Skip to main content

fiscal_core/
error.rs

1//! Error types for all fiscal operations.
2
3use thiserror::Error;
4
5/// The primary error type returned by all public functions in `fiscal-core`,
6/// `fiscal-crypto`, and `fiscal-sefaz`.
7///
8/// All variants are `#[non_exhaustive]` so new error cases can be added in
9/// future releases without breaking downstream `match` arms.
10#[derive(Debug, Error, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum FiscalError {
13    /// A tax data field contains an illegal value (e.g. malformed CNPJ, wrong
14    /// field length, invalid access-key format).
15    #[error("Invalid tax data: {0}")]
16    InvalidTaxData(String),
17
18    /// An ICMS CST code is not recognised by the current tax computation module.
19    #[error("Unsupported ICMS CST: {0}")]
20    UnsupportedIcmsCst(String),
21
22    /// An ICMS CSOSN code is not recognised by the current tax computation module.
23    #[error("Unsupported ICMS CSOSN: {0}")]
24    UnsupportedIcmsCsosn(String),
25
26    /// A required XML field is absent. The `field` member names the missing tag.
27    #[error("Required tax field \"{field}\" is missing")]
28    MissingRequiredField {
29        /// Name of the required XML tag or parameter that was absent.
30        field: String,
31    },
32
33    /// A GTIN barcode value is malformed or has an invalid check digit.
34    #[error("Invalid GTIN: {0}")]
35    InvalidGtin(String),
36
37    /// An error occurred while building or serialising an XML document.
38    #[error("XML generation failed: {0}")]
39    XmlGeneration(String),
40
41    /// An error occurred while parsing an XML document.
42    #[error("XML parsing failed: {0}")]
43    XmlParsing(String),
44
45    /// SEFAZ returned a rejection status code for a submitted document or event.
46    #[error("SEFAZ rejected: [{code}] {message}")]
47    SefazRejection {
48        /// SEFAZ status code (`cStat`), e.g. `"301"`.
49        code: String,
50        /// Human-readable rejection message (`xMotivo`).
51        message: String,
52    },
53
54    /// An error occurred while loading, parsing, or using a PKCS#12 certificate.
55    #[error("Certificate error: {0}")]
56    Certificate(String),
57
58    /// A two-letter state abbreviation (UF) or IBGE numeric code was not found
59    /// in the lookup table.
60    #[error("Invalid state code: {0}")]
61    InvalidStateCode(String),
62
63    /// An error occurred while activating, loading, or applying contingency mode.
64    #[error("Contingency error: {0}")]
65    Contingency(String),
66
67    /// A TXT document is structurally invalid or references an unsupported layout.
68    #[error("Invalid TXT: {0}")]
69    InvalidTxt(String),
70
71    /// The supplied document is not of the expected type (e.g. not a valid
72    /// NFe TXT header).
73    #[error("Wrong document: {0}")]
74    WrongDocument(String),
75
76    /// An HTTP or network-level error occurred during SEFAZ communication.
77    #[error("Network error: {0}")]
78    Network(String),
79}