From 7a111e9a8d4e447c89054725e9e8624b95c7fbcd Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Thu, 29 May 2025 11:39:40 -0700 Subject: [PATCH 1/2] fix(errors): pass through client error status codes in BackendError handling Updated the ResponseError implementation for BackendError to correctly pass through client error status codes from ApiClientError, ensuring that the appropriate status code is returned in the response. --- src/utils/errors.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils/errors.rs b/src/utils/errors.rs index 6c72ac4..ce93b9a 100644 --- a/src/utils/errors.rs +++ b/src/utils/errors.rs @@ -98,6 +98,11 @@ impl error::ResponseError for BackendError { fn status_code(&self) -> StatusCode { match self { + // Pass through client error status codes + BackendError::ApiClientError { status, .. } => { + StatusCode::from_u16(*status).unwrap_or(StatusCode::BAD_REQUEST) + } + // 400 BackendError::InvalidRequest(_) | BackendError::UnsupportedAuthMethod(_) @@ -114,7 +119,6 @@ impl error::ResponseError for BackendError { // 502 BackendError::ReqwestError(_) | BackendError::ApiServerError { .. } - | BackendError::ApiClientError { .. } | BackendError::RepositoryPermissionsNotFound | BackendError::AzureError(_) | BackendError::S3Error(_) => StatusCode::BAD_GATEWAY, From f9d481ab7c066b758a942974d868038e77f23e4e Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Thu, 29 May 2025 11:58:20 -0700 Subject: [PATCH 2/2] fix/update tests --- src/utils/errors.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/utils/errors.rs b/src/utils/errors.rs index ce93b9a..a47b674 100644 --- a/src/utils/errors.rs +++ b/src/utils/errors.rs @@ -512,7 +512,7 @@ mod tests { } #[test] - fn should_handle_api_client_error() { + fn should_handle_api_client_error_400() { let error = BackendError::ApiClientError { url: "https://api.example.com".to_string(), status: 400, @@ -520,8 +520,26 @@ mod tests { }; assert_eq!( error.status_code(), - StatusCode::BAD_GATEWAY, - "expected status code to be 502" + StatusCode::BAD_REQUEST, + "expected status code to be 400" + ); + assert!( + error.to_string().contains("api threw a client error"), + "expected error message to mention client error" + ); + } + + #[test] + fn should_handle_api_client_error_404() { + let error = BackendError::ApiClientError { + url: "https://api.example.com".to_string(), + status: 404, + message: "Bad Request".to_string(), + }; + assert_eq!( + error.status_code(), + StatusCode::NOT_FOUND, + "expected status code to be 404" ); assert!( error.to_string().contains("api threw a client error"),