fix: address review findings
This commit is contained in:
17
src/error.rs
17
src/error.rs
@@ -3,6 +3,8 @@ use serde::Serialize;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AppError {
|
||||
#[error("bad request: {0}")]
|
||||
BadRequest(String),
|
||||
#[error("pokemon not found")]
|
||||
NotFound,
|
||||
#[error("upstream service returned invalid data: {0}")]
|
||||
@@ -19,6 +21,7 @@ impl AppError {
|
||||
#[must_use]
|
||||
pub const fn status_code(&self) -> StatusCode {
|
||||
match self {
|
||||
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
|
||||
Self::NotFound => StatusCode::NOT_FOUND,
|
||||
Self::InvalidUpstreamData(_) | Self::Upstream(_) | Self::Timeout => {
|
||||
StatusCode::BAD_GATEWAY
|
||||
@@ -32,12 +35,24 @@ impl IntoResponse for AppError {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
let status = self.status_code();
|
||||
let body = ErrorBody {
|
||||
error: self.to_string(),
|
||||
error: self.public_message(),
|
||||
};
|
||||
(status, Json(body)).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
impl AppError {
|
||||
fn public_message(&self) -> String {
|
||||
match self {
|
||||
Self::BadRequest(message) => message.clone(),
|
||||
Self::NotFound => "pokemon not found".to_owned(),
|
||||
Self::InvalidUpstreamData(_) => "upstream service returned invalid data".to_owned(),
|
||||
Self::Upstream(_) | Self::Timeout => "upstream service failed".to_owned(),
|
||||
Self::Internal => "internal server error".to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ErrorBody {
|
||||
error: String,
|
||||
|
||||
Reference in New Issue
Block a user