Skip to content

Commit 83f42f4

Browse files
Pass std::exception_ptr to SendJsonError
The `diagnostic_information` string will now generated if and when it is required (i.e. the "verbose" parameter is true).
1 parent 1a52530 commit 83f42f4

14 files changed

Lines changed: 21 additions & 33 deletions

lib/remote/actionshandler.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ bool ActionsHandler::HandleRequest(
5656
objs = FilterUtility::GetFilterTargets(qd, params, user);
5757
} catch (const std::exception& ex) {
5858
HttpUtility::SendJsonError(response, params, 404,
59-
"No objects found.",
60-
DiagnosticInformation(ex));
59+
"No objects found.", std::current_exception());
6160
return true;
6261
}
6362

lib/remote/configfileshandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool ConfigFilesHandler::HandleRequest(
8686
throw;
8787
}
8888

89-
HttpUtility::SendJsonError(response, params, 500, "Could not read file.", DiagnosticInformation(ex));
89+
HttpUtility::SendJsonError(response, params, 500, "Could not read file.", std::current_exception());
9090
}
9191

9292
return true;

lib/remote/configpackageshandler.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ void ConfigPackagesHandler::HandleGet(const HttpApiRequest& request, HttpApiResp
5555
try {
5656
packages = ConfigPackageUtility::GetPackages();
5757
} catch (const std::exception& ex) {
58-
HttpUtility::SendJsonError(response, params, 500, "Could not retrieve packages.",
59-
DiagnosticInformation(ex));
58+
HttpUtility::SendJsonError(response, params, 500, "Could not retrieve packages.", std::current_exception());
6059
return;
6160
}
6261

@@ -121,8 +120,7 @@ void ConfigPackagesHandler::HandlePost(const HttpApiRequest& request, HttpApiRes
121120

122121
ConfigPackageUtility::CreatePackage(packageName);
123122
} catch (const std::exception& ex) {
124-
HttpUtility::SendJsonError(response, params, 500, "Could not create package '" + packageName + "'.",
125-
DiagnosticInformation(ex));
123+
HttpUtility::SendJsonError(response, params, 500, "Could not create package '" + packageName + "'.", std::current_exception());
126124
return;
127125
}
128126

@@ -169,8 +167,7 @@ void ConfigPackagesHandler::HandleDelete(const HttpApiRequest& request, HttpApiR
169167
try {
170168
ConfigPackageUtility::DeletePackage(packageName);
171169
} catch (const std::exception& ex) {
172-
HttpUtility::SendJsonError(response, params, 500, "Failed to delete package '" + packageName + "'.",
173-
DiagnosticInformation(ex));
170+
HttpUtility::SendJsonError(response, params, 500, "Failed to delete package '" + packageName + "'.", std::current_exception());
174171
return;
175172
}
176173

lib/remote/configstageshandler.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ void ConfigStagesHandler::HandlePost(const HttpApiRequest& request, HttpApiRespo
174174
ConfigPackageUtility::AsyncTryActivateStage(packageName, stageName, activate, reload, resetPackageUpdates);
175175
} catch (const std::exception& ex) {
176176
return HttpUtility::SendJsonError(response, params, 500,
177-
"Stage creation failed.",
178-
DiagnosticInformation(ex));
177+
"Stage creation failed.", std::current_exception());
179178
}
180179

181180

@@ -237,7 +236,7 @@ void ConfigStagesHandler::HandleDelete(const HttpApiRequest& request, HttpApiRes
237236
} catch (const std::exception& ex) {
238237
return HttpUtility::SendJsonError(response, params, 500,
239238
"Failed to delete stage '" + stageName + "' in package '" + packageName + "'.",
240-
DiagnosticInformation(ex));
239+
std::current_exception());
241240
}
242241

243242
Dictionary::Ptr result1 = new Dictionary({

lib/remote/deleteobjecthandler.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ bool DeleteObjectHandler::HandleRequest(
6060
objs = FilterUtility::GetFilterTargets(qd, params, user);
6161
} catch (const std::exception& ex) {
6262
HttpUtility::SendJsonError(response, params, 404,
63-
"No objects found.",
64-
DiagnosticInformation(ex));
63+
"No objects found.", std::current_exception());
6564
return true;
6665
}
6766

lib/remote/httpserverconnection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ void ProcessRequest(
438438
throw;
439439
}
440440

441-
HttpUtility::SendJsonError(response, request.Params(), 500, "Unhandled exception", DiagnosticInformation(ex));
441+
HttpUtility::SendJsonError(response, request.Params(), 500, "Unhandled exception", std::current_exception());
442442
}
443443

444444
response.Flush(yc);

lib/remote/httputility.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ void HttpUtility::SendJsonBody(HttpApiResponse& response, const Dictionary::Ptr&
8484
}
8585

8686
void HttpUtility::SendJsonError(HttpApiResponse& response,
87-
const Dictionary::Ptr& params, int code, const String& info, const String& diagnosticInformation)
87+
const Dictionary::Ptr& params, int code, const String& info, const std::exception_ptr& ex)
8888
{
8989
if (response.HasSerializationStarted()) {
9090
std::ostringstream err;
9191
err << "Impossible to send error response after streaming has started: error: '" << code << "', status: '"
9292
<< info << "'";
93-
if (!diagnosticInformation.IsEmpty()) {
94-
err << ", diagnostic_information: '" << diagnosticInformation << "'";
93+
if (ex) {
94+
err << ", diagnostic_information: '" << DiagnosticInformation(ex) << "'";
9595
}
9696
BOOST_THROW_EXCEPTION(std::logic_error{err.str()});
9797
}
@@ -102,8 +102,8 @@ void HttpUtility::SendJsonError(HttpApiResponse& response,
102102
result->Set("status", info);
103103
}
104104

105-
if (params && HttpUtility::GetLastParameter(params, "verbose") && !diagnosticInformation.IsEmpty()) {
106-
result->Set("diagnostic_information", diagnosticInformation);
105+
if (params && HttpUtility::GetLastParameter(params, "verbose") && ex) {
106+
result->Set("diagnostic_information", DiagnosticInformation(ex));
107107
}
108108

109109
response.Clear();

lib/remote/httputility.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class HttpUtility
2828
static void SendJsonBody(HttpApiResponse& response, const Dictionary::Ptr& params, const Value& val, boost::asio::yield_context& yc);
2929
static void SendJsonBody(HttpApiResponse& response, const Dictionary::Ptr& params, const Value& val);
3030
static void SendJsonError(HttpApiResponse& response, const Dictionary::Ptr& params, const int code,
31-
const String& info = {}, const String& diagnosticInformation = {});
31+
const String& info = {}, const std::exception_ptr& ex = nullptr);
3232

3333
static bool IsValidHeaderName(std::string_view name);
3434
static bool IsValidHeaderValue(std::string_view value);

lib/remote/modifyobjecthandler.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ bool ModifyObjectHandler::HandleRequest(
5858
objs = FilterUtility::GetFilterTargets(qd, params, user);
5959
} catch (const std::exception& ex) {
6060
HttpUtility::SendJsonError(response, params, 404,
61-
"No objects found.",
62-
DiagnosticInformation(ex));
61+
"No objects found.", std::current_exception());
6362
return true;
6463
}
6564

lib/remote/objectqueryhandler.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ bool ObjectQueryHandler::HandleRequest(
180180
objs = FilterUtility::GetFilterTargets(qd, params, user);
181181
} catch (const std::exception& ex) {
182182
HttpUtility::SendJsonError(response, params, 404,
183-
"No objects found.",
184-
DiagnosticInformation(ex));
183+
"No objects found.", std::current_exception());
185184
return true;
186185
}
187186

0 commit comments

Comments
 (0)