Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand All @@ -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,
Expand Down Expand Up @@ -508,16 +512,34 @@ 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,
message: "Bad Request".to_string(),
};
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"),
Expand Down