Better error response messages
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Ley 2022-07-20 11:57:38 +02:00
parent 1577b9a1ca
commit e367c341b7

View file

@ -8,7 +8,7 @@ use axum::{
use reqwest::StatusCode;
use super::models::ErrorResponse;
use crate::error::APIError;
use crate::error::{APIError, ServiceError};
impl ErrorResponse {
fn new(status: StatusCode, message: Option<String>) -> Self {
@ -31,13 +31,21 @@ impl IntoResponse for APIError {
sqlx::Error::RowNotFound => ErrorResponse::new(StatusCode::NOT_FOUND, None),
_ => ErrorResponse::new(StatusCode::INTERNAL_SERVER_ERROR, None),
},
APIError::Multipart(_) => ErrorResponse::new(StatusCode::INTERNAL_SERVER_ERROR, None),
APIError::Multipart(_) => ErrorResponse::new(
StatusCode::BAD_REQUEST,
Some("Invalid Multipart Form".to_string()),
),
APIError::BadRequest(err) => ErrorResponse::new(StatusCode::BAD_REQUEST, Some(err)),
APIError::Unauthorized(err) => ErrorResponse::new(StatusCode::UNAUTHORIZED, Some(err)),
APIError::Forbidden(err) => ErrorResponse::new(StatusCode::FORBIDDEN, Some(err)),
APIError::NotFound(err) => ErrorResponse::new(StatusCode::NOT_FOUND, Some(err)),
APIError::Internal(err) => ErrorResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Some(err)),
APIError::Service(_) => ErrorResponse::new(StatusCode::INTERNAL_SERVER_ERROR, None),
APIError::Internal(err) => {
ErrorResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Some(err))
}
APIError::Service(err) => ErrorResponse::new(
StatusCode::INTERNAL_SERVER_ERROR,
Some(err.get_response_message()),
),
APIError::Query(_) => ErrorResponse::new(StatusCode::BAD_REQUEST, None),
APIError::Decode(_) => ErrorResponse::new(StatusCode::BAD_REQUEST, None),
};
@ -45,3 +53,13 @@ impl IntoResponse for APIError {
(status, Json(res)).into_response()
}
}
impl ServiceError {
fn get_response_message(&self) -> String {
match self {
ServiceError::Reqwest(_) => "Reqwest error".to_string(),
ServiceError::Url(_) => "URL parse error".to_string(),
ServiceError::InvalidResponse(code) => format!("Invalid response code: {}", code),
}
}
}