aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-03-14 13:41:26 +0100
committermetamuffin <metamuffin@disroot.org>2025-03-14 13:41:26 +0100
commita76cab9f30a51b9b4d2377b2d25640c129bf3ab3 (patch)
treebef8f43659c1e48eda7a1c2ce68207881a8d7e41
parentcdc2e8c14824a5d58154c5df42d1bc79495d69ea (diff)
downloadgnix-a76cab9f30a51b9b4d2377b2d25640c129bf3ab3.tar
gnix-a76cab9f30a51b9b4d2377b2d25640c129bf3ab3.tar.bz2
gnix-a76cab9f30a51b9b4d2377b2d25640c129bf3ab3.tar.zst
send better error status codes
-rw-r--r--src/error.rs34
-rw-r--r--src/main.rs2
2 files changed, 34 insertions, 2 deletions
diff --git a/src/error.rs b/src/error.rs
index c7290af..a518bfb 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -8,7 +8,7 @@ pub enum ServiceError {
Limit(#[from] tokio::sync::TryAcquireError),
#[error("hyper error")]
Hyper(hyper::Error),
- #[error("no host")]
+ #[error("host header missing")]
NoHost,
#[error("unknown host")]
UnknownHost,
@@ -49,3 +49,35 @@ pub enum ServiceError {
#[error("impossible error")]
Other,
}
+
+impl ServiceError {
+ pub fn status_code(&self) -> http::StatusCode {
+ use http::StatusCode;
+ match self {
+ ServiceError::NoResponse => todo!(),
+ ServiceError::RequestTaken => StatusCode::INTERNAL_SERVER_ERROR,
+ ServiceError::Limit(_) => StatusCode::TOO_MANY_REQUESTS,
+ ServiceError::Hyper(_) => StatusCode::INTERNAL_SERVER_ERROR,
+ ServiceError::NoHost => StatusCode::BAD_REQUEST,
+ ServiceError::UnknownHost => StatusCode::NOT_FOUND,
+ ServiceError::UnknownPath => StatusCode::NOT_FOUND,
+ ServiceError::CantConnect => StatusCode::BAD_GATEWAY,
+ ServiceError::NotFound => StatusCode::NOT_FOUND,
+ ServiceError::Io(_) => StatusCode::BAD_REQUEST,
+ ServiceError::BadRange => StatusCode::BAD_REQUEST,
+ ServiceError::BadUtf8(_) => StatusCode::BAD_REQUEST,
+ ServiceError::BadUtf82(_) => StatusCode::BAD_REQUEST,
+ ServiceError::BadUtf83(_) => StatusCode::BAD_REQUEST,
+ ServiceError::BadPath => StatusCode::BAD_REQUEST,
+ ServiceError::BadAuth => StatusCode::UNAUTHORIZED,
+ ServiceError::BadBase64(_) => StatusCode::BAD_REQUEST,
+ ServiceError::UpgradeFailed => StatusCode::UPGRADE_REQUIRED,
+ ServiceError::Custom(_) => StatusCode::BAD_REQUEST,
+ ServiceError::CustomStatic(_) => StatusCode::BAD_REQUEST,
+ ServiceError::ParseIntError(_) => StatusCode::BAD_REQUEST,
+ ServiceError::InvalidHeader => StatusCode::BAD_REQUEST,
+ ServiceError::InvalidUri => StatusCode::BAD_REQUEST,
+ ServiceError::Other => StatusCode::INTERNAL_SERVER_ERROR,
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index b42d868..d8e1407 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -203,7 +203,7 @@ pub async fn serve_stream<T: Unpin + Send + 'static + hyper::rt::Read + hyper::r
let mut resp = Response::new(format!(
"Sorry, we were unable to process your request: {error}"
));
- *resp.status_mut() = StatusCode::BAD_REQUEST;
+ *resp.status_mut() = error.status_code();
resp.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("text/plain"));
resp.headers_mut()