Skip to content
Merged
Changes from 1 commit
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
51 changes: 36 additions & 15 deletions rust/server/src/vss_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,50 @@ async fn handle_request<
}

fn build_error_response(e: VssError) -> Response<Full<Bytes>> {
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(),
_ => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if you don't mind let's be explicit about the error here I prefer to have them written out explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I think the general idea is that InternalServerError will be the catchall error type for 'unknown' errors, so a wildcare here makes sense if we'd ever add new error types. But spelled it out for now

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()
Expand Down