From 3ec1485ae7856e76db8eb2f305ef5950e60f7d8c Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Mon, 30 May 2022 00:01:39 -0400 Subject: errors/handlers: Make 404 errors more clear Instead of returning a 500 for every error, this should make it a little easier to return specific status codes for different errors --- src/errors.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/errors.rs') diff --git a/src/errors.rs b/src/errors.rs index 42477c0..c1d6672 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -12,6 +12,9 @@ pub enum ServiceError { #[error("SQL error: {0}")] Sql(#[from] sqlx::Error), + + #[error("Not Found")] + NotFound, } pub type StringResult = Result; @@ -22,8 +25,13 @@ impl IntoResponse for ServiceError { fn into_response(self) -> Response { let body = body::boxed(body::Full::from(self.to_string())); + let status = match self { + ServiceError::NotFound => StatusCode::NOT_FOUND, + _ => StatusCode::INTERNAL_SERVER_ERROR, + }; + Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) + .status(status) .body(body) .unwrap() } -- cgit v1.2.3