booking_kit/
error.rs

1//! Module for handling errors related to the booking system.
2
3use thiserror::Error;
4use crate::model::status::BookingStatus;
5
6/// Enum used to represent errors that occur during the booking process.
7#[derive(Debug, Error)]
8pub enum BookingError {
9    /// Error when the item with ID {0} is unavailable.
10    #[error("Booking item with ID {0} is unavailable.")]
11    ItemUnavailable(String),
12
13    /// Error when the booking status is invalid, e.g., an unregistered status.
14    #[error("Booking status is invalid: {0}")]
15    InvalidStatus(String),
16
17    /// Error when a transition between statuses is not allowed.
18    #[error("Invalid status transition from {from:?} to {to:?}")]
19    InvalidStatusTransition {
20        from: BookingStatus,
21        to: BookingStatus,
22    },
23
24    /// Error when booking creation fails, e.g., due to an internal error.
25    #[error("Failed to create booking: {0}")]
26    CreationFailed(String),
27
28    /// Error when booking rule validation fails.
29    #[error("Booking rule validation failed: {0}")]
30    RuleValidationFailed(String),
31
32    /// Error when the booking item quantity exceeds the available limit.
33    #[error("Booking item quantity exceeds available limit.")]
34    QuantityExceeded,
35
36    /// General error that doesn't fit other categories.
37    #[error("General error: {0}")]
38    GeneralError(String),
39}
40
41impl BookingError {
42    /// Creates an InvalidStatus error with the specific status message.
43    pub fn new_invalid_status(status: &str) -> Self {
44        BookingError::InvalidStatus(status.to_string())
45    }
46
47    /// Creates an ItemUnavailable error with the item ID that is unavailable.
48    pub fn new_item_unavailable(item_id: &str) -> Self {
49        BookingError::ItemUnavailable(item_id.to_string())
50    }
51
52    /// Creates an InvalidStatusTransition error with the provided from and to status.
53    ///
54    /// This error is returned when an attempt is made to transition between two booking statuses
55    /// in an invalid or unsupported way. It helps to catch scenarios where a booking cannot be 
56    /// moved from one status to another due to business logic constraints.
57    ///
58    /// # Parameters
59    /// - `from`: The current status of the booking.
60    /// - `to`: The status the booking is trying to transition to.
61    ///
62    /// # Example
63    /// ```rust
64    /// use booking_kit::error::BookingError;
65    /// use booking_kit::model::status::BookingStatus;
66    /// let error = BookingError::new_invalid_transition(BookingStatus::Pending, BookingStatus::Expired);
67    /// assert_eq!(error.to_string(), "Invalid status transition from Pending to Expired");
68    /// ```
69    pub fn new_invalid_transition(from: BookingStatus, to: BookingStatus) -> Self {
70        BookingError::InvalidStatusTransition { from, to }
71    }
72
73    /// Creates a CreationFailed error with a specific failure message.
74    pub fn new_creation_failed(message: &str) -> Self {
75        BookingError::CreationFailed(message.to_string())
76    }
77
78    /// Creates a RuleValidationFailed error with a specific validation failure message.
79    pub fn new_rule_validation_failed(message: &str) -> Self {
80        BookingError::RuleValidationFailed(message.to_string())
81    }
82
83    /// Creates a QuantityExceeded error.
84    pub fn new_quantity_exceeded() -> Self {
85        BookingError::QuantityExceeded
86    }
87
88    /// Creates a GeneralError with a specific message.
89    pub fn new_general_error(message: &str) -> Self {
90        BookingError::GeneralError(message.to_string())
91    }
92}