use anyhow::anyhow; 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("Channel full")] ChannelFullErr, #[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::ChannelFullErr => { sentry_anyhow::capture_anyhow(&anyhow!("Max concurrent tasks reached")); ( StatusCode::SERVICE_UNAVAILABLE, "Max concurrent tasks reached".to_string(), ) } AppError::Other(err) => { sentry_anyhow::capture_anyhow(&err); ( StatusCode::INTERNAL_SERVER_ERROR, "Internal server error".to_string(), ) } } .into_response() } }