Handling http errors a bit more gracefully

This commit is contained in:
Ian Roddis
2021-12-30 14:29:56 -04:00
parent d9479f14e0
commit a4b26bce04

View File

@@ -276,17 +276,17 @@ namespace daggy {
auto response = HTTP_REQUEST(url, payload, method);
rj::Document doc;
if (response.code == HTTPCode::Server_Error) {
doc.SetObject();
auto &alloc = doc.GetAllocator();
doc.AddMember("error",
rj::Value().SetString(response.body.c_str(),
response.body.size(), alloc),
alloc);
}
else {
try {
checkRJParse(doc.Parse(response.body.c_str()));
}
catch (std::exception &e) {
doc.SetObject();
auto &alloc = doc.GetAllocator();
std::string message = (response.body.empty() ? e.what() : response.body);
doc.AddMember(
"error",
rj::Value().SetString(message.c_str(), message.size(), alloc), alloc);
}
return std::make_pair(response.code, std::move(doc));
}