From 5f792f6590dbc4d13a2c8b1613307fb0c302f8ca Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 5 Nov 2025 14:12:07 +0100 Subject: [PATCH] Use appropriate HTTP status codes in error responses Previously, we would just always return `StatusCode::INTERNAL_SERVER_ERROR`. Here we fix this behavior and implment what we did in the now-deprecated Java version. --- rust/server/src/vss_service.rs | 51 ++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/rust/server/src/vss_service.rs b/rust/server/src/vss_service.rs index fecba40..cf9cc90 100644 --- a/rust/server/src/vss_service.rs +++ b/rust/server/src/vss_service.rs @@ -129,29 +129,50 @@ async fn handle_request< } fn build_error_response(e: VssError) -> Response> { - let error_response = match e { - VssError::NoSuchKeyError(msg) => ErrorResponse { - error_code: ErrorCode::NoSuchKeyException.into(), - message: msg.to_string(), + let (status_code, error_response) = match e { + VssError::NoSuchKeyError(msg) => { + let status = StatusCode::NOT_FOUND; + let error = ErrorResponse { + error_code: ErrorCode::NoSuchKeyException.into(), + message: msg.to_string(), + }; + (status, error) }, - VssError::ConflictError(msg) => ErrorResponse { - error_code: ErrorCode::ConflictException.into(), - message: msg.to_string(), + VssError::ConflictError(msg) => { + let status = StatusCode::CONFLICT; + let error = ErrorResponse { + error_code: ErrorCode::ConflictException.into(), + message: msg.to_string(), + }; + (status, error) }, - VssError::InvalidRequestError(msg) => ErrorResponse { - error_code: ErrorCode::InvalidRequestException.into(), - message: msg.to_string(), + VssError::InvalidRequestError(msg) => { + let status = StatusCode::BAD_REQUEST; + let error = ErrorResponse { + error_code: ErrorCode::InvalidRequestException.into(), + message: msg.to_string(), + }; + (status, error) }, VssError::AuthError(msg) => { - ErrorResponse { error_code: ErrorCode::AuthException.into(), message: msg.to_string() } + let status = StatusCode::UNAUTHORIZED; + let error = ErrorResponse { + error_code: ErrorCode::AuthException.into(), + message: msg.to_string(), + }; + (status, error) }, - _ => ErrorResponse { - error_code: ErrorCode::InternalServerException.into(), - message: "Unknown Server Error occurred.".to_string(), + _ => { + let status = StatusCode::INTERNAL_SERVER_ERROR; + let error = ErrorResponse { + error_code: ErrorCode::InternalServerException.into(), + message: "Unknown Server Error occurred.".to_string(), + }; + (status, error) }, }; Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) + .status(status_code) .body(Full::new(Bytes::from(error_response.encode_to_vec()))) // unwrap safety: body only errors when previous chained calls failed. .unwrap()