| 1 |
use axum::http::StatusCode; |
| 2 |
use axum::response::{IntoResponse, Response}; |
| 3 |
|
| 4 |
#[derive(Debug, thiserror::Error)] |
| 5 |
pub enum Error { |
| 6 |
#[error("not found")] |
| 7 |
NotFound, |
| 8 |
#[error("bad request: {0}")] |
| 9 |
BadRequest(String), |
| 10 |
#[error(transparent)] |
| 11 |
Db(#[from] sqlx::Error), |
| 12 |
#[error(transparent)] |
| 13 |
Other(#[from] anyhow::Error), |
| 14 |
} |
| 15 |
|
| 16 |
impl IntoResponse for Error { |
| 17 |
fn into_response(self) -> Response { |
| 18 |
let status = match &self { |
| 19 |
Error::NotFound => StatusCode::NOT_FOUND, |
| 20 |
Error::BadRequest(_) => StatusCode::BAD_REQUEST, |
| 21 |
_ => StatusCode::INTERNAL_SERVER_ERROR, |
| 22 |
}; |
| 23 |
(status, self.to_string()).into_response() |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
pub type Result<T> = std::result::Result<T, Error>; |
| 28 |
|