Skip to content

Commit ca99f1c

Browse files
Hansel Ipclaude
authored andcommitted
Address PR #1405 review: use parse with exceptions disabled
Replace double-parsing approach (accept() then parse()) with single parse() call using allow_exceptions=false and is_discarded() check. This eliminates redundant parsing and removes the need for try/catch. Addresses review comments from lalitb and ThomsonTan. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 374a971 commit ca99f1c

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

lib/http/HttpResponseDecoder.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,26 @@ namespace MAT_NS_BEGIN {
169169
{
170170
#ifdef HAVE_MAT_JSONHPP
171171
// TODO: [MG] - parse HTTP response without json.hpp library
172-
nlohmann::json responseBody;
173-
try
172+
std::string body(response.GetBody().begin(), response.GetBody().end());
173+
174+
// Handle empty body
175+
if (body.empty())
174176
{
175-
std::string body(response.GetBody().begin(), response.GetBody().end());
177+
LOG_ERROR("HTTP response: body is empty, skipping processing");
178+
return;
179+
}
176180

177-
// Validate that the body is valid JSON before attempting to parse
178-
if (body.empty() || !nlohmann::json::accept(body))
179-
{
180-
LOG_ERROR("HTTP response: body is not valid JSON, skipping processing");
181-
return;
182-
}
181+
// Parse JSON with exceptions disabled
182+
nlohmann::json responseBody = nlohmann::json::parse(body, nullptr, false);
183+
184+
// Check if parsing failed (returns discarded value for invalid JSON)
185+
if (responseBody.is_discarded())
186+
{
187+
LOG_ERROR("HTTP response: body is not valid JSON, skipping processing");
188+
return;
189+
}
183190

184-
responseBody = nlohmann::json::parse(body.c_str());
191+
{
185192
int accepted = 0;
186193
auto acc = responseBody.find("acc");
187194
if (responseBody.end() != acc)
@@ -240,10 +247,6 @@ namespace MAT_NS_BEGIN {
240247
LOG_TRACE("HTTP response: all rejected");
241248
}
242249
}
243-
catch (...)
244-
{
245-
LOG_ERROR("HTTP response: JSON parsing failed");
246-
}
247250
#else
248251
UNREFERENCED_PARAMETER(response);
249252
UNREFERENCED_PARAMETER(result);

0 commit comments

Comments
 (0)