diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/error.rs | 34 | ||||
-rw-r--r-- | src/main.rs | 2 |
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() |