algohub_server/models/
error.rs

1use rocket::{serde::json::Json, Responder};
2use serde::Serialize;
3
4#[derive(Serialize)]
5pub struct ErrorResponse {
6    pub success: bool,
7    pub message: String,
8}
9
10impl<T: ToString> From<T> for ErrorResponse {
11    fn from(message: T) -> Self {
12        Self {
13            success: false,
14            message: message.to_string(),
15        }
16    }
17}
18
19#[derive(Responder)]
20pub enum Error {
21    #[response(status = 500, content_type = "json")]
22    ServerError(Json<ErrorResponse>),
23    #[response(status = 400, content_type = "json")]
24    BadRequest(Json<ErrorResponse>),
25    #[response(status = 401, content_type = "json")]
26    Unauthorized(Json<ErrorResponse>),
27    #[response(status = 404, content_type = "json")]
28    NotFound(Json<ErrorResponse>),
29    #[response(status = 405, content_type = "json")]
30    MethodNotAllowed(Json<ErrorResponse>),
31    #[response(status = 409, content_type = "json")]
32    Conflict(Json<ErrorResponse>),
33    #[response(status = 403, content_type = "json")]
34    Forbidden(Json<ErrorResponse>),
35}
36
37impl<T: ToString> From<T> for Error {
38    fn from(e: T) -> Self {
39        Error::ServerError(Json(e.into()))
40    }
41}