Skip to main content

stateset_core/errors/
customer.rs

1//! Customer-domain errors.
2
3use uuid::Uuid;
4
5/// Errors specific to customer operations.
6///
7/// # Example
8///
9/// ```rust
10/// use stateset_core::errors::CustomerError;
11///
12/// let err = CustomerError::not_found(uuid::Uuid::nil());
13/// assert!(err.to_string().contains("not found"));
14/// ```
15#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum CustomerError {
18    /// Customer with the given ID was not found.
19    #[error("customer not found: {0}")]
20    NotFound(Uuid),
21
22    /// Email address is already registered.
23    #[error("email already exists: {0}")]
24    EmailAlreadyExists(String),
25
26    /// Customer account is not active.
27    #[error("customer is not active")]
28    NotActive,
29
30    /// Extensibility.
31    #[error(transparent)]
32    Other(Box<dyn std::error::Error + Send + Sync>),
33}
34
35impl CustomerError {
36    /// Convenience constructor for `NotFound`.
37    #[inline]
38    #[track_caller]
39    #[must_use]
40    pub const fn not_found(id: Uuid) -> Self {
41        Self::NotFound(id)
42    }
43
44    /// Convenience constructor for `EmailAlreadyExists`.
45    #[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}