//! Maps `fil_core::Error` onto HTTP responses for the API server. use axum::{http::StatusCode, response::IntoResponse}; pub enum ApiError { Kasten(fil_core::Error), Serialization(serde_json::Error), Io(std::io::Error), NotFound(String), BadRequest(String), } impl From for ApiError { fn from(err: std::io::Error) -> Self { Self::Io(err) } } impl From for ApiError { fn from(err: fil_core::Error) -> Self { Self::Kasten(err) } } impl From for ApiError { fn from(err: serde_json::Error) -> Self { Self::Serialization(err) } } impl IntoResponse for ApiError { fn into_response(self) -> axum::response::Response { match self { Self::Kasten(err) => { let status = match &err { fil_core::Error::GroupDoesNotExist => StatusCode::NOT_FOUND, fil_core::Error::InvalidDueDate { .. } | fil_core::Error::InvalidPriority => { StatusCode::BAD_REQUEST } _ => StatusCode::INTERNAL_SERVER_ERROR, }; (status, err.to_string()).into_response() } Self::Serialization(err) => { (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response() } Self::Io(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response(), Self::NotFound(msg) => (StatusCode::NOT_FOUND, msg).into_response(), Self::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg).into_response(), } } }