Skip to content

Commit

Permalink
Throw custom exception in HttpClient (#1641)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwc0919 committed Jun 24, 2023
1 parent 8e4474b commit eea9163
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lib/inc/drogon/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class DROGON_EXPORT HttpClient : public trantor::NonCopyable
*
* @param req
* @param timeout In seconds. If the response is not received within the
* timeout, A `std::runtime_error` with the message "Timeout" is thrown.
* timeout, A `drogon::HttpException` with `ReqResult::Timeout` is thrown.
* The zero value by default disables the timeout.
*
* @return internal::HttpRespAwaiter. Await on it to get the response
Expand Down Expand Up @@ -346,24 +346,41 @@ class DROGON_EXPORT HttpClient : public trantor::NonCopyable
};

#ifdef __cpp_impl_coroutine

class HttpException : public std::exception
{
public:
HttpException() = delete;
explicit HttpException(ReqResult res)
: resultCode_(res), message_(to_string_view(res))
{
}
const char *what() const noexcept override
{
return message_.data();
}
ReqResult code() const
{
return resultCode_;
}

private:
ReqResult resultCode_;
std::string_view message_;
};

inline void internal::HttpRespAwaiter::await_suspend(
std::coroutine_handle<> handle)
{
assert(client_ != nullptr);
assert(req_ != nullptr);
client_->sendRequest(
req_,
[handle = std::move(handle), this](ReqResult result,
const HttpResponsePtr &resp) {
[handle, this](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
setValue(resp);
else
{
std::stringstream ss;
ss << result;
setException(
std::make_exception_ptr(std::runtime_error(ss.str())));
}
setException(std::make_exception_ptr(HttpException(result)));
handle.resume();
},
timeout_);
Expand Down

0 comments on commit eea9163

Please sign in to comment.