stateset_core/errors/
shipping.rs1use uuid::Uuid;
4
5#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum ShippingError {
18 #[error("shipment not found: {0}")]
20 NotFound(Uuid),
21
22 #[error("carrier error: {reason}")]
24 CarrierError {
25 carrier: String,
27 reason: String,
29 },
30
31 #[error("invalid tracking number: {0}")]
33 InvalidTrackingNumber(String),
34
35 #[error(transparent)]
37 Other(Box<dyn std::error::Error + Send + Sync>),
38}
39
40impl ShippingError {
41 #[inline]
43 #[track_caller]
44 #[must_use]
45 pub const fn not_found(id: Uuid) -> Self {
46 Self::NotFound(id)
47 }
48
49 #[track_caller]
51 pub fn carrier_error(carrier: impl Into<String>, reason: impl Into<String>) -> Self {
52 Self::CarrierError { carrier: carrier.into(), reason: reason.into() }
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn not_found_display() {
62 let err = ShippingError::not_found(Uuid::nil());
63 assert!(err.to_string().contains("not found"));
64 }
65
66 #[test]
67 fn carrier_error_display() {
68 let err = ShippingError::carrier_error("FedEx", "address invalid");
69 assert!(err.to_string().contains("address invalid"));
70 }
71
72 #[test]
73 fn converts_to_commerce_error() {
74 use crate::errors::CommerceError;
75
76 let ship_err = ShippingError::not_found(Uuid::nil());
77 let commerce_err: CommerceError = ship_err.into();
78 assert!(commerce_err.is_not_found());
79 }
80}