oxidite_core/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum Error {
5    #[error("Internal server error: {0}")]
6    InternalServerError(String),
7    #[error("Server error: {0}")]
8    Server(String),
9    #[error("Resource not found: {0}")]
10    NotFound(String),
11    #[error("Bad request: {0}")]
12    BadRequest(String),
13    #[error("Unauthorized access: {0}")]
14    Unauthorized(String),
15    #[error("Forbidden: {0}")]
16    Forbidden(String),
17    #[error("Resource conflict: {0}")]
18    Conflict(String),
19    #[error("Validation failed: {0}")]
20    Validation(String),
21    #[error("Rate limit exceeded: {0}")]
22    RateLimited(String),
23    #[error("Service temporarily unavailable: {0}")]
24    ServiceUnavailable(String),
25    #[error(transparent)]
26    Hyper(#[from] hyper::Error),
27    #[error(transparent)]
28    Io(#[from] std::io::Error),
29    #[error(transparent)]
30    SerdeJson(#[from] serde_json::Error),
31    #[error(transparent)]
32    SerdeUrlEncoded(#[from] serde_urlencoded::de::Error),
33    #[error(transparent)]
34    Http(#[from] http::Error),
35}
36
37/// A specialized Result type for Oxidite applications
38pub type Result<T> = std::result::Result<T, Error>;
39
40impl Error {
41    /// Get the HTTP status code for this error
42    pub fn status_code(&self) -> hyper::StatusCode {
43        match self {
44            Error::NotFound(_) => hyper::StatusCode::NOT_FOUND,
45            Error::BadRequest(_) => hyper::StatusCode::BAD_REQUEST,
46            Error::Unauthorized(_) => hyper::StatusCode::UNAUTHORIZED,
47            Error::Forbidden(_) => hyper::StatusCode::FORBIDDEN,
48            Error::Conflict(_) => hyper::StatusCode::CONFLICT,
49            Error::Validation(_) => hyper::StatusCode::UNPROCESSABLE_ENTITY,
50            Error::RateLimited(_) => hyper::StatusCode::TOO_MANY_REQUESTS,
51            Error::ServiceUnavailable(_) => hyper::StatusCode::SERVICE_UNAVAILABLE,
52            Error::InternalServerError(_) | Error::Server(_) | Error::Hyper(_) | Error::Io(_) | Error::SerdeJson(_) | 
53            Error::SerdeUrlEncoded(_) | Error::Http(_) => hyper::StatusCode::INTERNAL_SERVER_ERROR,
54        }
55    }
56}