ping_viewer_next/server/protocols/v1/
errors.rs

1use actix_web::{http::StatusCode, ResponseError};
2
3use paperclip::actix::api_v2_errors;
4use validator::ValidationErrors;
5
6#[allow(dead_code)]
7#[api_v2_errors(
8    code = 400,
9    description = "Bad Request: The client's request contains invalid or malformed data.",
10    code = 500,
11    description = "Internal Server Error: An unexpected server error has occurred."
12)]
13#[derive(Debug, thiserror::Error)]
14pub enum Error {
15    #[error("Bad Request: {0}")]
16    BadRequest(String),
17    #[error("Internal Server Error: {0}")]
18    Internal(String),
19}
20
21impl ResponseError for Error {
22    fn status_code(&self) -> StatusCode {
23        match self {
24            Self::BadRequest(_) => StatusCode::BAD_REQUEST,
25            Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
26        }
27    }
28}
29
30impl From<ValidationErrors> for Error {
31    fn from(error: ValidationErrors) -> Self {
32        Self::BadRequest(error.to_string())
33    }
34}
35
36impl From<crate::device::manager::ManagerError> for Error {
37    fn from(error: crate::device::manager::ManagerError) -> Self {
38        Self::Internal(serde_json::to_string_pretty(&error).unwrap_or_default())
39    }
40}