monzo-ingestion/src/error.rs

39 lines
1.0 KiB
Rust

use axum::response::{IntoResponse, Response};
use http::StatusCode;
use sea_orm::DbErr;
use tracing::log::error;
#[derive(thiserror::Error, Debug)]
pub enum AppError {
/// SeaORM error, separated for ease of use allowing us to `?` db operations.
#[error("Database error: {0}")]
DbError(#[from] DbErr),
#[error("Invalid request {0}")]
BadRequest(anyhow::Error),
/// Catch all for error we don't care to expose publicly.
#[error("An error occurred: {0}")]
Anyhow(#[from] anyhow::Error),
}
impl AppError {
fn to_response_string(&self) -> String {
match self {
AppError::BadRequest(e) => e.to_string(),
_ => "Internal server error".to_string(),
}
}
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let status_code = match self {
AppError::BadRequest(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
(status_code, self.to_response_string()).into_response()
}
}