Skip to content

Commit 197b41e

Browse files
committed
refactor: separe errors into a module
1 parent a1b65a8 commit 197b41e

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

src/errors.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use axum::http::StatusCode;
2+
use axum::response::{IntoResponse, Response};
3+
4+
#[derive(Debug)]
5+
pub enum HeaderValidationError {
6+
MissingHeader(&'static str),
7+
InvalidSignature,
8+
InvalidUserAgent,
9+
WebhookNotFound,
10+
AxumError(axum::Error),
11+
}
12+
13+
impl IntoResponse for HeaderValidationError {
14+
fn into_response(self) -> Response {
15+
let (status, message) = match self {
16+
HeaderValidationError::MissingHeader(header) => (
17+
StatusCode::BAD_REQUEST,
18+
format!("Missing required header: {header}"),
19+
),
20+
HeaderValidationError::InvalidSignature => {
21+
(StatusCode::UNAUTHORIZED, "Invalid signature".to_string())
22+
}
23+
HeaderValidationError::InvalidUserAgent => {
24+
(StatusCode::BAD_REQUEST, "Invalid User-Agent".to_string())
25+
}
26+
HeaderValidationError::WebhookNotFound => {
27+
(StatusCode::NOT_FOUND, "Webhook not configured".to_string())
28+
}
29+
HeaderValidationError::AxumError(error) => (StatusCode::BAD_REQUEST, error.to_string()),
30+
};
31+
(status, message).into_response()
32+
}
33+
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use notify::{EventHandler, EventKind, Watcher};
88
use tokio::sync::RwLock;
99
use tracing::level_filters::LevelFilter;
1010

11+
mod errors;
1112
mod handlers;
1213
mod validator;
1314

src/validator.rs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,14 @@
11
use std::usize;
22

3-
use crate::GlobalConfig;
3+
use crate::{GlobalConfig, errors::HeaderValidationError};
44
use axum::{
55
extract::{Path, Request, State},
6-
http::{HeaderMap, StatusCode, header},
6+
http::{HeaderMap, header},
77
middleware::Next,
8-
response::{IntoResponse, Response},
8+
response::Response,
99
};
1010
use hmac::{Hmac, Mac};
1111

12-
#[derive(Debug)]
13-
pub enum HeaderValidationError {
14-
MissingHeader(&'static str),
15-
InvalidSignature,
16-
InvalidUserAgent,
17-
WebhookNotFound,
18-
AxumError(axum::Error),
19-
}
20-
21-
impl IntoResponse for HeaderValidationError {
22-
fn into_response(self) -> Response {
23-
let (status, message) = match self {
24-
HeaderValidationError::MissingHeader(header) => (
25-
StatusCode::BAD_REQUEST,
26-
format!("Missing required header: {header}"),
27-
),
28-
HeaderValidationError::InvalidSignature => {
29-
(StatusCode::UNAUTHORIZED, "Invalid signature".to_string())
30-
}
31-
HeaderValidationError::InvalidUserAgent => {
32-
(StatusCode::BAD_REQUEST, "Invalid User-Agent".to_string())
33-
}
34-
HeaderValidationError::WebhookNotFound => {
35-
(StatusCode::NOT_FOUND, "Webhook not configured".to_string())
36-
}
37-
HeaderValidationError::AxumError(error) => (StatusCode::BAD_REQUEST, error.to_string()),
38-
};
39-
(status, message).into_response()
40-
}
41-
}
42-
4312
pub async fn validate_headers(
4413
request: Request,
4514
next: Next,
@@ -132,7 +101,7 @@ fn validate_signature(
132101
let expected_signature = hex::encode(mac.finalize().into_bytes());
133102
(expected_signature, signature)
134103
}
135-
None => {
104+
_ => {
136105
let signature = headers
137106
.get("X-Hub-Signature")
138107
.and_then(|v| v.to_str().ok())

0 commit comments

Comments
 (0)