diff options
Diffstat (limited to 'src/zenhttp/httpclient.cpp')
| -rw-r--r-- | src/zenhttp/httpclient.cpp | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/zenhttp/httpclient.cpp b/src/zenhttp/httpclient.cpp index 05ff6d07b..744787201 100644 --- a/src/zenhttp/httpclient.cpp +++ b/src/zenhttp/httpclient.cpp @@ -435,7 +435,28 @@ HttpClient::Response::ToText() bool HttpClient::Response::IsSuccess() const noexcept { - return IsHttpSuccessCode(StatusCode); + return !Error && IsHttpSuccessCode(StatusCode); +} + +void +HttpClient::Response::ThrowError(std::string_view ErrorPrefix) +{ + if (!IsSuccess()) + { + if (Error.has_value()) + { + throw std::runtime_error(fmt::format("{}: {}", ErrorPrefix, Error->ErrorMessage)); + } + else if (StatusCode != HttpResponseCode::ImATeapot && (int)StatusCode) + { + throw std::runtime_error( + fmt::format("{}: HTTP error {} {} ({})", ErrorPrefix, (int)StatusCode, zen::ToString(StatusCode), AsText())); + } + else + { + throw std::runtime_error(fmt::format("{}: {}", ErrorPrefix, "unknown error")); + } + } } ////////////////////////////////////////////////////////////////////////// |