improve webhook parsing

This commit is contained in:
2026-06-01 21:10:45 +00:00
parent 7e3b49ad76
commit 0a22be252c
6 changed files with 193 additions and 284 deletions
+25 -35
View File
@@ -3,24 +3,24 @@ use reqwest::StatusCode;
#[derive(thiserror::Error, Debug)]
pub enum AppError {
#[error("Unauthorized user id")]
UnauthorizedUserIdErr,
#[error("Unknow gitea event")]
UnknownEventErr,
#[error("Malformed Json")]
MalformedJsonErr,
#[error("Json not contains mandatory fields")]
BadJsonStructErr,
#[error(transparent)]
BadJsonStructErr(#[from] serde_json::Error),
#[error("WebHook sig header not found")]
WebHookSigHeaderNotFoundErr,
#[error("WebHook header not found")]
WebHookMissingHeaderErr(String),
#[error("WebHook sig header is invalid")]
WebHookSigHeaderInvalidErr,
#[error("Missing required field: {0}")]
MissingField(String),
#[error("Wrong type for field: {0}")]
WrongFieldType(String),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
@@ -28,39 +28,29 @@ pub enum AppError {
impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
match self {
AppError::MalformedJsonErr => {
(StatusCode::BAD_REQUEST, "Malformed Json".to_string()).into_response()
AppError::UnknownEventErr => {
(StatusCode::BAD_REQUEST, "Unknow gitea event".to_string())
}
AppError::BadJsonStructErr => (
AppError::UnauthorizedUserIdErr => {
(StatusCode::BAD_REQUEST, "Unauthorized user id".to_string())
}
AppError::MalformedJsonErr => (StatusCode::BAD_REQUEST, "Malformed Json".to_string()),
AppError::BadJsonStructErr(err) => (
StatusCode::BAD_REQUEST,
"Json not contains mandatory fields".to_string(),
)
.into_response(),
AppError::WebHookSigHeaderNotFoundErr => (
StatusCode::BAD_REQUEST,
"WebHook sig header not found".to_string(),
)
.into_response(),
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(),
)
.into_response(),
AppError::MissingField(ref field) => (
StatusCode::BAD_REQUEST,
format!("Missing required field: {}", field),
)
.into_response(),
AppError::WrongFieldType(ref field) => (
StatusCode::BAD_REQUEST,
format!("Wrong type for field: {}", field),
)
.into_response(),
),
AppError::Other(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
"Internal server error".to_string(),
)
.into_response(),
),
}
.into_response()
}
}