soph_server/response/
error.rsuse crate::error::Error;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
impl IntoResponse for Error {
fn into_response(self) -> Response {
let (status, message) = match self {
#[cfg(feature = "request-auth")]
Self::Auth(error) => (StatusCode::UNAUTHORIZED, error.to_string()),
#[cfg(feature = "request-form")]
Self::FormRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
#[cfg(feature = "request-path")]
Self::PathRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
#[cfg(feature = "request-query")]
Self::QueryRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
#[cfg(feature = "request-json")]
Self::JsonRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
#[cfg(feature = "request-multipart")]
Self::MultipartRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
#[cfg(feature = "request-multipart")]
Self::Multipart(error) => (StatusCode::BAD_REQUEST, error.to_string()),
#[cfg(feature = "request-validate")]
Self::Validate(error) => (
StatusCode::BAD_REQUEST,
format!("Validation error: {}", error),
),
#[cfg(feature = "database")]
Self::SeaOrm(error) => {
let status = match error {
sea_orm::DbErr::RecordNotFound(_) => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
(status, error.to_string())
}
#[cfg(feature = "response-view")]
Self::Tera(error) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("View error: {error}"),
),
_ => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
};
tracing::warn!(error = message, "response");
#[cfg(not(feature = "response-json"))]
{
(status, message).into_response()
}
#[cfg(feature = "response-json")]
{
crate::response::json()
.code(status)
.message(message)
.into_response()
}
}
}