stateset_core/errors/
customer.rs1use uuid::Uuid;
4
5#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum CustomerError {
18 #[error("customer not found: {0}")]
20 NotFound(Uuid),
21
22 #[error("email already exists: {0}")]
24 EmailAlreadyExists(String),
25
26 #[error("customer is not active")]
28 NotActive,
29
30 #[error(transparent)]
32 Other(Box<dyn std::error::Error + Send + Sync>),
33}
34
35impl CustomerError {
36 #[inline]
38 #[track_caller]
39 #[must_use]
40 pub const fn not_found(id: Uuid) -> Self {
41 Self::NotFound(id)
42 }
43
44 #[track_caller]
46 pub fn email_already_exists(email: impl Into<String>) -> Self {
47 Self::EmailAlreadyExists(email.into())
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn not_found_display() {
57 let err = CustomerError::not_found(Uuid::nil());
58 assert!(err.to_string().contains("not found"));
59 }
60
61 #[test]
62 fn email_exists_display() {
63 let err = CustomerError::email_already_exists("alice@example.com");
64 assert!(err.to_string().contains("alice@example.com"));
65 }
66
67 #[test]
68 fn not_active_display() {
69 let err = CustomerError::NotActive;
70 assert_eq!(err.to_string(), "customer is not active");
71 }
72
73 #[test]
74 fn converts_to_commerce_error() {
75 use crate::errors::CommerceError;
76
77 let cust_err = CustomerError::not_found(Uuid::nil());
78 let commerce_err: CommerceError = cust_err.into();
79 assert!(commerce_err.is_not_found());
80
81 let dup_err = CustomerError::email_already_exists("x@y.com");
82 let commerce_err: CommerceError = dup_err.into();
83 assert!(commerce_err.is_conflict());
84 }
85}