Skip to content

Commit efaacf4

Browse files
authored
impl(rest): map http code 504 to kUnavailable (#16077)
1 parent 471d12d commit efaacf4

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

google/cloud/internal/rest_response.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ StatusCode MapHttpCodeToStatus5xx(std::int32_t code) {
120120
if (code == HttpStatusCode::kServiceUnavailable) {
121121
return StatusCode::kUnavailable;
122122
}
123+
if (code == HttpStatusCode::kGatewayTimeout) {
124+
return StatusCode::kUnavailable;
125+
}
123126
// 5XX - server errors are mapped to kInternal.
124127
return StatusCode::kInternal;
125128
}
@@ -153,8 +156,12 @@ Status AsStatus(HttpStatusCode http_status_code, std::string payload) {
153156
if (payload.empty()) {
154157
// If there's no payload, create one to make sure the original http status
155158
// code received is available.
156-
return Status(status_code, "Received HTTP status code: " +
157-
std::to_string(http_status_code));
159+
ErrorInfo error_info{
160+
{}, {}, {{"http_status_code", std::to_string(http_status_code)}}};
161+
return Status(
162+
status_code,
163+
"Received HTTP status code: " + std::to_string(http_status_code),
164+
std::move(error_info));
158165
}
159166
auto p =
160167
ParseJsonError(static_cast<int>(http_status_code), std::move(payload));

google/cloud/internal/rest_response.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ enum HttpStatusCode : std::int32_t {
6767
kInternalServerError = 500,
6868
kBadGateway = 502,
6969
kServiceUnavailable = 503,
70+
kGatewayTimeout = 504,
7071
};
7172

7273
// This class contains the results of making a request to a RESTful service.

google/cloud/internal/rest_response_test.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ INSTANTIATE_TEST_SUITE_P(
8383
std::make_pair(HttpStatusCode::kBadGateway, StatusCode::kUnavailable),
8484
std::make_pair(HttpStatusCode::kServiceUnavailable,
8585
StatusCode::kUnavailable),
86-
std::make_pair(static_cast<HttpStatusCode>(504), StatusCode::kInternal),
86+
std::make_pair(HttpStatusCode::kGatewayTimeout,
87+
StatusCode::kUnavailable),
8788
std::make_pair(static_cast<HttpStatusCode>(601), StatusCode::kUnknown)),
8889
[](testing::TestParamInfo<MapHttpCodeToStatusTest::ParamType> const& info) {
8990
return std::to_string(std::get<0>(info.param));
@@ -135,6 +136,8 @@ TEST(AsStatus, RestResponseIsNotOkNoPayload) {
135136
EXPECT_THAT(status, StatusIs(StatusCode::kNotFound));
136137
EXPECT_THAT(status.message(), Eq("Received HTTP status code: 404"));
137138
EXPECT_TRUE(status.error_info().reason().empty());
139+
EXPECT_THAT(status.error_info().metadata(),
140+
Contains(::testing::Pair("http_status_code", "404")));
138141
}
139142

140143
TEST(AsStatus, RestResponseIsNotOkPayload) {

0 commit comments

Comments
 (0)