Skip to content

Commit

Permalink
update error types
Browse files Browse the repository at this point in the history
GitOrigin-RevId: fd77dbda5b580cf62723e10dc3056ac5eeeaccbf
  • Loading branch information
gautamg795 authored and Convex, Inc. committed Sep 24, 2024
1 parent 7985643 commit 65e97e2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
27 changes: 24 additions & 3 deletions crates/errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub enum ErrorCode {
OutOfRetention,

OperationalInternalServerError,
MisdirectedRequest,
}

impl ErrorMetadata {
Expand Down Expand Up @@ -147,6 +148,17 @@ impl ErrorMetadata {
}
}

/// Conductor recevied a request intended for an instance it does not serve.
/// This error is intended to be used by Usher to retry the request and/or
/// update its caches, and should not directly be sent back to the user.
pub fn misdirected_request() -> Self {
Self {
code: ErrorCode::MisdirectedRequest,
short_msg: "MisdirectedRequest".into(),
msg: "Instance not served by this Conductor".into(),
}
}

/// RateLimited. Maps to 429 in HTTP.
///
/// The short_msg should be a CapitalCamelCased describing the error (eg
Expand Down Expand Up @@ -351,7 +363,8 @@ impl ErrorMetadata {
| ErrorCode::OCC
| ErrorCode::OutOfRetention
| ErrorCode::Overloaded
| ErrorCode::RejectedBeforeExecution => false,
| ErrorCode::RejectedBeforeExecution
| ErrorCode::MisdirectedRequest => false,
}
}

Expand All @@ -367,7 +380,8 @@ impl ErrorMetadata {
| ErrorCode::TransientNotFound
| ErrorCode::PaginationLimit
| ErrorCode::Unauthenticated
| ErrorCode::Forbidden => Some((sentry::Level::Info, None)),
| ErrorCode::Forbidden
| ErrorCode::MisdirectedRequest => Some((sentry::Level::Info, None)),
ErrorCode::OutOfRetention
| ErrorCode::Overloaded
| ErrorCode::RejectedBeforeExecution
Expand All @@ -386,6 +400,7 @@ impl ErrorMetadata {
| ErrorCode::Unauthenticated
| ErrorCode::Forbidden
| ErrorCode::ClientDisconnect
| ErrorCode::MisdirectedRequest
| ErrorCode::RateLimited => None,
ErrorCode::TransientNotFound => Some("transient_not_found"),
ErrorCode::OCC => Some("occ"),
Expand Down Expand Up @@ -415,6 +430,7 @@ impl ErrorMetadata {
ErrorCode::Overloaded => None,
ErrorCode::RejectedBeforeExecution => None,
ErrorCode::OperationalInternalServerError => None,
ErrorCode::MisdirectedRequest => None,
}
}

Expand All @@ -428,7 +444,8 @@ impl ErrorMetadata {
| ErrorCode::OutOfRetention
| ErrorCode::Overloaded
| ErrorCode::RateLimited
| ErrorCode::RejectedBeforeExecution => Some(CloseCode::Again),
| ErrorCode::RejectedBeforeExecution
| ErrorCode::MisdirectedRequest => Some(CloseCode::Again),
ErrorCode::OperationalInternalServerError => Some(CloseCode::Error),
// These ones are client errors - so no close code - the client
// will handle and close the connection instead.
Expand Down Expand Up @@ -464,6 +481,7 @@ impl ErrorCode {
| ErrorCode::Overloaded
| ErrorCode::RejectedBeforeExecution => StatusCode::SERVICE_UNAVAILABLE,
ErrorCode::ClientDisconnect => StatusCode::REQUEST_TIMEOUT,
ErrorCode::MisdirectedRequest => StatusCode::MISDIRECTED_REQUEST,
}
}

Expand All @@ -481,6 +499,7 @@ impl ErrorCode {
ErrorCode::PaginationLimit => tonic::Code::InvalidArgument,
ErrorCode::OutOfRetention => tonic::Code::OutOfRange,
ErrorCode::OperationalInternalServerError => tonic::Code::Internal,
ErrorCode::MisdirectedRequest => tonic::Code::InvalidArgument,
}
}

Expand All @@ -490,6 +509,7 @@ impl ErrorCode {
StatusCode::FORBIDDEN => Some(ErrorCode::Forbidden),
StatusCode::NOT_FOUND => Some(ErrorCode::TransientNotFound),
StatusCode::TOO_MANY_REQUESTS => Some(ErrorCode::RateLimited),
StatusCode::MISDIRECTED_REQUEST => Some(ErrorCode::MisdirectedRequest),
// Tries to categorize in one of the above more specific 4xx codes first,
// otherwise categorizes as a general 4xx via BadRequest
v if v.is_client_error() => Some(ErrorCode::BadRequest),
Expand Down Expand Up @@ -792,6 +812,7 @@ mod proptest {
ErrorMetadata::operational_internal_server_error()
},
ErrorCode::ClientDisconnect => ErrorMetadata::client_disconnect(),
ErrorCode::MisdirectedRequest => ErrorMetadata::misdirected_request(),
})
}
}
Expand Down
25 changes: 13 additions & 12 deletions crates/pb/protos/errors.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ syntax = "proto3";
package errors;

enum ErrorCode {
BAD_REQUEST = 0;
UNAUTHENTICATED = 1;
FORBIDDEN = 2;
TRANSIENT_NOT_FOUND = 3;
CLIENT_DISCONNECT = 4;
RATE_LIMITED = 11;
OVERLOADED = 5;
REJECTED_BEFORE_EXECUTION = 10;
OCC = 6;
PAGINATION_LIMIT = 7;
OUT_OF_RETENTION = 8;
OPERATIONAL_INTERNAL_SERVER_ERROR = 9;
BAD_REQUEST = 0;
UNAUTHENTICATED = 1;
FORBIDDEN = 2;
TRANSIENT_NOT_FOUND = 3;
CLIENT_DISCONNECT = 4;
OVERLOADED = 5;
OCC = 6;
PAGINATION_LIMIT = 7;
OUT_OF_RETENTION = 8;
OPERATIONAL_INTERNAL_SERVER_ERROR = 9;
REJECTED_BEFORE_EXECUTION = 10;
RATE_LIMITED = 11;
MISDIRECTED_REQUEST = 12;
}

message ErrorMetadata {
Expand Down
2 changes: 2 additions & 0 deletions crates/pb/src/error_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl From<ErrorCode> for ErrorCodeProto {
ErrorCode::OperationalInternalServerError => {
ErrorCodeProto::OperationalInternalServerError
},
ErrorCode::MisdirectedRequest => ErrorCodeProto::MisdirectedRequest,
}
}
}
Expand All @@ -51,6 +52,7 @@ impl From<ErrorCodeProto> for ErrorCode {
ErrorCodeProto::OperationalInternalServerError => {
ErrorCode::OperationalInternalServerError
},
ErrorCodeProto::MisdirectedRequest => ErrorCode::MisdirectedRequest,
}
}
}
Expand Down

0 comments on commit 65e97e2

Please sign in to comment.