@@ -46,24 +46,29 @@ nlohmann::json HttpClient::PostJson(const std::string& path,
4646
4747void HttpClient::GetRawStream (const std::string& path,
4848 const httplib::Params& params,
49- httplib::ContentReceiver callback) {
49+ const httplib::ContentReceiver& callback) {
5050 const std::string full_path = httplib::append_query_params (path, params);
51+ std::string err_body{};
5152 int err_status{};
5253 httplib::Result res = client_.Get (
5354 full_path,
5455 [&err_status](const httplib::Response& resp) {
55- // only continue if good response status
5656 if (HttpClient::IsErrorStatus (resp.status )) {
57- // instead of throwing here, store the HTTP status and return false to
58- // have the client close the connection
5957 err_status = resp.status ;
60- return false ;
6158 }
6259 return true ;
6360 },
64- std::move (callback));
61+ [&callback, &err_body, &err_status](const char * data,
62+ std::size_t length) {
63+ // if an error response was received, read all content into err_status
64+ if (err_status > 0 ) {
65+ err_body.append (data, length);
66+ return true ;
67+ }
68+ return callback (data, length);
69+ });
6570 if (err_status > 0 ) {
66- throw HttpResponseError{path, err_status, " " };
71+ throw HttpResponseError{path, err_status, std::move (err_body) };
6772 }
6873 if (res.error () != httplib::Error::Success) {
6974 throw HttpRequestError{path, res.error ()};
0 commit comments