diff --git a/src/utils/errors.rs b/src/utils/errors.rs index 6c72ac4..a47b674 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, @@ -508,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, @@ -516,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"),