Skip to content

Commit 62e53b0

Browse files
committed
feat s3api: include body in the s3 exception
Tests: CI commit_hash:d6c1a7e597bf1bed73154c5d4c7644c107ff14bf
1 parent 67bc626 commit 62e53b0

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

core/include/userver/clients/http/response.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,19 @@ class Response final {
5151
bool IsError() const { return static_cast<uint16_t>(status_code()) >= 400; }
5252

5353
static void RaiseForStatus(int code, const LocalStats& stats);
54+
static void RaiseForStatus(int code, const LocalStats& stats, std::string_view message);
5455

55-
void raise_for_status() const;
56+
/// @brief Configuration whether to include the response body in the exception in @ref raise_for_status.
57+
enum class RaiseIncludeBody : std::uint8_t { kNo = 0, kYes = 1 };
5658

57-
/// returns statistics on request execution like count of opened sockets,
58-
/// connect time...
59+
/// @brief Raise an exception depending on the response status.
60+
/// The body of the response may be included in the exception depending on the @param include_body.
61+
///
62+
/// @throws HttpClientException for statuses [400; 500)
63+
/// @throws HttpServerException for statuses [500; 600)
64+
void raise_for_status(RaiseIncludeBody include_body = RaiseIncludeBody::kNo) const;
65+
66+
/// returns statistics on request execution like count of opened sockets, connect time...
5967
LocalStats GetStats() const;
6068

6169
void SetStats(const LocalStats& stats) { stats_ = stats; }

core/src/clients/http/response.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <userver/clients/http/response.hpp>
22

3+
#include <fmt/core.h>
4+
35
#include <userver/clients/http/response_future.hpp>
6+
#include <userver/utils/assert.hpp>
47

58
USERVER_NAMESPACE_BEGIN
69

@@ -16,7 +19,25 @@ void Response::RaiseForStatus(int code, const LocalStats& stats) {
1619
}
1720
}
1821

19-
void Response::raise_for_status() const { RaiseForStatus(status_code(), GetStats()); }
22+
void Response::RaiseForStatus(int code, const LocalStats& stats, std::string_view message) {
23+
if (400 <= code && code < 500) {
24+
throw HttpClientException(code, stats, message);
25+
} else if (500 <= code && code < 600) {
26+
throw HttpServerException(code, stats, message);
27+
}
28+
}
29+
30+
void Response::raise_for_status(RaiseIncludeBody include_body) const {
31+
switch (include_body) {
32+
case RaiseIncludeBody::kNo:
33+
RaiseForStatus(status_code(), GetStats());
34+
return;
35+
case RaiseIncludeBody::kYes:
36+
RaiseForStatus(status_code(), GetStats(), body());
37+
return;
38+
}
39+
UINVARIANT(false, fmt::format("Unhandled 'include_body' value: {}", static_cast<int>(include_body)));
40+
}
2041

2142
LocalStats Response::GetStats() const { return stats_; }
2243

libraries/s3api/src/s3api/s3_connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ std::shared_ptr<clients::http::Response> S3Connection::RequestApi(Request& r, st
5858
std::shared_ptr<clients::http::Response> response;
5959
try {
6060
response = GetMethod(http_req, full_url, r.body, r.method).perform();
61-
response->raise_for_status();
61+
response->raise_for_status(clients::http::Response::RaiseIncludeBody::kYes);
6262
} catch (const clients::http::TimeoutException& e) {
6363
LOG_WARNING() << "S3Api : Http Request Timeout: " << full_url;
6464
throw;

0 commit comments

Comments
 (0)