mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
style: Use error code instead of exception when parsing json (#1942)
This commit is contained in:
@@ -54,7 +54,6 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <ratio>
|
#include <ratio>
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
@@ -129,30 +128,32 @@ public:
|
|||||||
&connectionMetadata,
|
&connectionMetadata,
|
||||||
subscriptionContext = std::move(subscriptionContext)](boost::asio::yield_context innerYield) mutable {
|
subscriptionContext = std::move(subscriptionContext)](boost::asio::yield_context innerYield) mutable {
|
||||||
try {
|
try {
|
||||||
auto parsedRequest = boost::json::parse(request.message()).as_object();
|
boost::system::error_code ec;
|
||||||
LOG(perfLog_.debug()) << connectionMetadata.tag() << "Adding to work queue";
|
auto parsedRequest = boost::json::parse(request.message(), ec);
|
||||||
|
if (ec.failed() or not parsedRequest.is_object()) {
|
||||||
|
rpcEngine_->notifyBadSyntax();
|
||||||
|
response = impl::ErrorHelper{request}.makeJsonParsingError();
|
||||||
|
if (ec.failed()) {
|
||||||
|
LOG(log_.warn())
|
||||||
|
<< "Error parsing JSON: " << ec.message() << ". For request: " << request.message();
|
||||||
|
} else {
|
||||||
|
LOG(log_.warn()) << "Received not a JSON object. For request: " << request.message();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto parsedObject = std::move(parsedRequest).as_object();
|
||||||
|
LOG(perfLog_.debug()) << connectionMetadata.tag() << "Adding to work queue";
|
||||||
|
|
||||||
if (not connectionMetadata.wasUpgraded() and shouldReplaceParams(parsedRequest))
|
if (not connectionMetadata.wasUpgraded() and shouldReplaceParams(parsedObject))
|
||||||
parsedRequest[JS(params)] = boost::json::array({boost::json::object{}});
|
parsedObject[JS(params)] = boost::json::array({boost::json::object{}});
|
||||||
|
|
||||||
response = handleRequest(
|
response = handleRequest(
|
||||||
innerYield,
|
innerYield,
|
||||||
request,
|
request,
|
||||||
std::move(parsedRequest),
|
std::move(parsedObject),
|
||||||
connectionMetadata,
|
connectionMetadata,
|
||||||
std::move(subscriptionContext)
|
std::move(subscriptionContext)
|
||||||
);
|
);
|
||||||
} catch (boost::system::system_error const& ex) {
|
}
|
||||||
// system_error thrown when json parsing failed
|
|
||||||
rpcEngine_->notifyBadSyntax();
|
|
||||||
response = impl::ErrorHelper{request}.makeJsonParsingError();
|
|
||||||
LOG(log_.warn()) << "Error parsing JSON: " << ex.what() << ". For request: " << request.message();
|
|
||||||
} catch (std::invalid_argument const& ex) {
|
|
||||||
// thrown when json parses something that is not an object at top level
|
|
||||||
rpcEngine_->notifyBadSyntax();
|
|
||||||
LOG(log_.warn()) << "Invalid argument error: " << ex.what()
|
|
||||||
<< ". For request: " << request.message();
|
|
||||||
response = impl::ErrorHelper{request}.makeJsonParsingError();
|
|
||||||
} catch (std::exception const& ex) {
|
} catch (std::exception const& ex) {
|
||||||
LOG(perfLog_.error()) << connectionMetadata.tag() << "Caught exception: " << ex.what();
|
LOG(perfLog_.error()) << connectionMetadata.tag() << "Caught exception: " << ex.what();
|
||||||
rpcEngine_->notifyInternalError();
|
rpcEngine_->notifyInternalError();
|
||||||
|
|||||||
Reference in New Issue
Block a user