Files
herald/src/errors.rs
T

65 lines
2.0 KiB
Rust

use axum::response::IntoResponse;
use reqwest::StatusCode;
#[derive(thiserror::Error, Debug)]
pub enum AppError {
#[error("Unauthorized user id")]
UnauthorizedUserErr,
#[error("Unknow gitea event")]
UnknownEventErr,
#[error("Malformed Json")]
MalformedJsonErr,
#[error("WebHook header not found")]
WebHookMissingHeaderErr(String),
#[error("WebHook sig header is invalid")]
WebHookSigHeaderInvalidErr,
#[error("WebHook have bad action")]
InvalidActionErr,
#[error(transparent)]
BadJsonStructErr(#[from] serde_json::Error),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
match self {
AppError::InvalidActionErr => (
StatusCode::UNPROCESSABLE_ENTITY,
"WebHook have bad action".to_string(),
),
AppError::UnknownEventErr => {
(StatusCode::BAD_REQUEST, "Unknow gitea event".to_string())
}
AppError::UnauthorizedUserErr => (
StatusCode::UNAUTHORIZED,
"Unauthorized user name".to_string(),
),
AppError::MalformedJsonErr => (StatusCode::BAD_REQUEST, "Malformed Json".to_string()),
AppError::BadJsonStructErr(err) => (
StatusCode::BAD_REQUEST,
format!("Json not contains mandatory fields: {}", err),
),
AppError::WebHookMissingHeaderErr(h) => {
(StatusCode::BAD_REQUEST, format!("header {} is missing", h))
}
AppError::WebHookSigHeaderInvalidErr => (
StatusCode::UNAUTHORIZED,
"WebHook sig header is invalid".to_string(),
),
AppError::Other(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
"Internal server error".to_string(),
),
}
.into_response()
}
}