refactor: improve error code handling in status code conversion (#5851)

* refactor: improve error code handling in status code conversion

* chore: apply suggestions from CR

* fix: only hanlde client side thrown error

* feat: introduce `DeadlineExceeded`

* fix: exclude Code::Unknown from retry conditions
This commit is contained in:
Weny Xu
2025-04-09 15:58:23 +08:00
committed by GitHub
parent 6c66ec3ffc
commit 746b4e2369
9 changed files with 53 additions and 32 deletions

View File

@@ -772,9 +772,14 @@ impl IntoResponse for Error {
}
}
/// Converts [StatusCode] to [HttpStatusCode].
pub fn status_code_to_http_status(status_code: &StatusCode) -> HttpStatusCode {
match status_code {
StatusCode::Success | StatusCode::Cancelled => HttpStatusCode::OK,
StatusCode::Success => HttpStatusCode::OK,
// When a request is cancelled by the client (e.g., by a client side timeout),
// we should return a gateway timeout status code to the external client.
StatusCode::Cancelled | StatusCode::DeadlineExceeded => HttpStatusCode::GATEWAY_TIMEOUT,
StatusCode::Unsupported
| StatusCode::InvalidArguments

View File

@@ -334,7 +334,7 @@ fn mysql_error_kind(status_code: &StatusCode) -> ErrorKind {
StatusCode::Success => ErrorKind::ER_YES,
StatusCode::Unknown | StatusCode::External => ErrorKind::ER_UNKNOWN_ERROR,
StatusCode::Unsupported => ErrorKind::ER_NOT_SUPPORTED_YET,
StatusCode::Cancelled => ErrorKind::ER_QUERY_INTERRUPTED,
StatusCode::Cancelled | StatusCode::DeadlineExceeded => ErrorKind::ER_QUERY_INTERRUPTED,
StatusCode::RuntimeResourcesExhausted => ErrorKind::ER_OUT_OF_RESOURCES,
StatusCode::InvalidSyntax => ErrorKind::ER_SYNTAX_ERROR,
StatusCode::RegionAlreadyExists | StatusCode::TableAlreadyExists => {

View File

@@ -374,6 +374,7 @@ impl From<StatusCode> for PgErrorCode {
StatusCode::Unsupported => PgErrorCode::Ec0A000,
StatusCode::InvalidArguments => PgErrorCode::Ec22023,
StatusCode::Cancelled => PgErrorCode::Ec57000,
StatusCode::DeadlineExceeded => PgErrorCode::Ec57000,
StatusCode::External => PgErrorCode::Ec58000,
StatusCode::Unknown