Skip to main content

reinhardt_views/viewsets/handler/
error.rs

1//! Error type used by the model-based ViewSet handler.
2
3/// Error type for `ModelViewSetHandler`.
4#[derive(Debug)]
5pub enum ViewError {
6	/// Serialization or deserialization failure.
7	Serialization(String),
8	/// Permission denied for the requested action.
9	Permission(String),
10	/// The requested resource was not found.
11	NotFound(String),
12	/// The request was malformed or invalid.
13	BadRequest(String),
14	/// An internal server error occurred.
15	Internal(String),
16	/// A database operation failed.
17	DatabaseError(String),
18}
19
20impl std::fmt::Display for ViewError {
21	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22		match self {
23			ViewError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
24			ViewError::Permission(msg) => write!(f, "Permission denied: {}", msg),
25			ViewError::NotFound(msg) => write!(f, "Not found: {}", msg),
26			ViewError::BadRequest(msg) => write!(f, "Bad request: {}", msg),
27			ViewError::Internal(msg) => write!(f, "Internal error: {}", msg),
28			ViewError::DatabaseError(msg) => write!(f, "Database error: {}", msg),
29		}
30	}
31}
32
33impl std::error::Error for ViewError {}
34
35/// Convert `ViewError` into the framework-wide `reinhardt_core::exception::Error`.
36///
37/// Mapping preserves HTTP status codes via `Error::status_code()`:
38///
39/// | `ViewError`        | `Error`         | Status |
40/// |--------------------|-----------------|--------|
41/// | `Serialization`    | `Serialization` | 400    |
42/// | `Permission`       | `Authorization` | 403    |
43/// | `NotFound`         | `NotFound`      | 404    |
44/// | `BadRequest`       | `Http`          | 400    |
45/// | `Internal`         | `Internal`      | 500    |
46/// | `DatabaseError`    | `Database`      | 500    |
47impl From<ViewError> for reinhardt_core::exception::Error {
48	fn from(value: ViewError) -> Self {
49		match value {
50			ViewError::Serialization(m) => Self::Serialization(m),
51			ViewError::Permission(m) => Self::Authorization(m),
52			ViewError::NotFound(m) => Self::NotFound(m),
53			ViewError::BadRequest(m) => Self::Http(m),
54			ViewError::Internal(m) => Self::Internal(m),
55			ViewError::DatabaseError(m) => Self::Database(m),
56		}
57	}
58}