Errors

One Error type: kind() says why, location() says where (0-based), rule() names the EN 16931 or Peppol rule it enforces. Match on those, not on the text.

On this page

Every fallible call returns billing::Error — one type for the whole crate. It has three parts:

AccessorAnswersType
kind()why it failed&ErrorKind (#[non_exhaustive])
location()where in your inputOption<&Location>
rule()which EN 16931 / Peppol rule it enforcesOption<&'static str>
use billing::{AmountScale, Currency, Document, ErrorKind, Line, Location, Price, Quantity, UnitPrice};
use rust_decimal::dec;

let err = Document::builder(Currency::EUR, AmountScale::EN16931)
    .line(Line::fixed("ok", dec!(10)))
    .line(Line::new("bad", Quantity::one(), UnitPrice::new(Price::new(dec!(-1)))))
    .build()
    .unwrap_err();

assert_eq!(err.kind(), &ErrorKind::NegativePrice { price: dec!(-1) });
assert_eq!(err.location(), Some(&Location::Line(1)));
assert_eq!(err.rule(), Some("BR-27"));
assert_eq!(err.to_string(), "line 1: price -1 is negative (BR-27)");

Match on the kind, the location and the rule, not on the Display text, which may be reworded. ErrorKind is #[non_exhaustive], so a match needs a wildcard arm:

use billing::{Error, ErrorKind};

fn advice(err: &Error) -> &'static str {
    match err.kind() {
        ErrorKind::Overflow => "the amounts are out of range",
        ErrorKind::InvalidVat(_) | ErrorKind::MissingVat => "check the VAT treatment",
        _ if err.rule().is_some() => "the input breaks an EN 16931 rule",
        _ => "invalid input",
    }
}
assert_eq!(advice(&ErrorKind::MissingVat.into()), "check the VAT treatment");

🔗Locations

Every index is 0-based — the position in your slice or builder call order — and Display prints that same index:

LocationDisplayPoints at
Line(0)line 0the first line
LineAdjustment { line: 1, index: 0 }line 1 adjustment 0the first allowance or charge of the second line
Adjustment(2)adjustment 2the third document allowance or charge
Tier(3)tier 3the fourth tier
Weight(4)weight 4the fifth split weight
Bound(0)bound 0the first Bound given to the builder
Payment(1)payment 1the second Payment given to the builder (doc.payments()[1])
Sample(2)sample 2the third reading given to an aggregation
Credit(0)credit 0the first Credit given to the builder
Transaction(3)transaction 3the fourth amount given to TransactionFee::rate

Document adjustments are numbered in builder order, followed by those materialised by apportion. A parse failure or a settlement setting has no location.

🔗Rules

rule() is Some exactly where an artefact rule exists for the failure:

KindRule
NegativePriceBR-27
InvalidBaseQuantityPEPPOL-EN16931-R121
AdjustmentBasisPEPPOL-EN16931-R040
PriceBaseUnitPEPPOL-EN16931-R130
InvalidVatAccountingPEPPOL-EN16931-R005
MissingVat at a Location::LineBR-CO-04
MissingVat at a document allowance / chargeBR-32 / BR-37
InvalidVat(VatError)per variant: BR-*-05 (rate), BR-*-10 (reason), BR-O-11, BR-B-02

Every other kind — arithmetic or domain validation with no artefact rule, including InvalidTaxPartition (stricter than BR-CO-17) — reports None. Every rule id returned is hash-locked against the pinned Schematron (just check-citations).

🔗The kinds, grouped

GroupKinds
arithmeticOverflow, ExcessPrecision, DivisionByZero, InvalidIncrement
textMalformed, InvalidCurrency
prices and quantitiesNegativePrice, InvalidBaseQuantity, PriceBaseUnit
pricingInvalidTiers(TierError), Uncovered, UnknownKey, InvalidPercentile, InvalidFee, InvalidRange
meteringMissingKey, NegativeWeight, InvalidConversion
splittingInvalidWeights
VATInvalidVat(VatError), MissingVat, ConflictingExemption, InvalidTaxPartition
adjustments and boundsAdjustmentBasis, InvalidBound
settlementInvalidCashRounding, InvalidVatAccounting, InvalidRate, InvalidCredit, UnmatchedAdvance
combiningCurrencyMismatch, ScaleMismatch, SettingMismatch, ConflictingSetting, Empty

Overflow is never approximated. A value with excess digits that is also out of range reports ExcessPrecision.

🔗Panics

The crate’s fallible functions never panic. Two things do, by contract:

  • the +, -, unary -, +=, -= and Sum operators on Amount, on overflow — use the checked_* methods for untrusted magnitudes;
  • the indexed accessors line_net(i), line_adjustment_amounts(i) and adjustment_amount(i), for an index out of range, like slice indexing.

Improve this page on GitHub