feat: add pokeapi client
This commit is contained in:
44
src/error.rs
Normal file
44
src/error.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use axum::{Json, http::StatusCode, response::IntoResponse};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AppError {
|
||||
#[error("pokemon not found")]
|
||||
NotFound,
|
||||
#[error("upstream service returned invalid data: {0}")]
|
||||
InvalidUpstreamData(String),
|
||||
#[error("upstream service failed: {0}")]
|
||||
Upstream(String),
|
||||
#[error("request timed out")]
|
||||
Timeout,
|
||||
#[error("internal server error")]
|
||||
Internal,
|
||||
}
|
||||
|
||||
impl AppError {
|
||||
#[must_use]
|
||||
pub const fn status_code(&self) -> StatusCode {
|
||||
match self {
|
||||
Self::NotFound => StatusCode::NOT_FOUND,
|
||||
Self::InvalidUpstreamData(_) | Self::Upstream(_) | Self::Timeout => {
|
||||
StatusCode::BAD_GATEWAY
|
||||
}
|
||||
Self::Internal => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
let status = self.status_code();
|
||||
let body = ErrorBody {
|
||||
error: self.to_string(),
|
||||
};
|
||||
(status, Json(body)).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ErrorBody {
|
||||
error: String,
|
||||
}
|
||||
Reference in New Issue
Block a user