From ed1b93420e5d95f12c1520f4664eac93cb3f4284 Mon Sep 17 00:00:00 2001 From: Sixtysixter <20945591+Sixtysixter@users.noreply.github.com> Date: Sat, 3 Feb 2024 22:38:20 +0100 Subject: [PATCH] rpcdaemon: json streamed request exception handling (#1790) --- silkworm/rpc/http/request_handler.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/silkworm/rpc/http/request_handler.cpp b/silkworm/rpc/http/request_handler.cpp index 0fdde71e57..792142f6f1 100644 --- a/silkworm/rpc/http/request_handler.cpp +++ b/silkworm/rpc/http/request_handler.cpp @@ -158,20 +158,27 @@ Task RequestHandler::handle_request(commands::RpcApiTable::HandleMethod ha } Task RequestHandler::handle_request(commands::RpcApiTable::HandleStream handler, const nlohmann::json& request_json) { - try { - auto io_executor = co_await boost::asio::this_coro::executor; + auto io_executor = co_await boost::asio::this_coro::executor; + try { co_await channel_->open_stream(); ChunkWriter chunk_writer(*channel_); json::Stream stream(io_executor, chunk_writer); - co_await (rpc_api_.*handler)(request_json, stream); - + try { + co_await (rpc_api_.*handler)(request_json, stream); + } catch (const std::exception& e) { + SILK_ERROR << "exception: " << e.what(); + const auto error = make_json_error(request_json, 100, e.what()); + stream.write_json(error); + } catch (...) { + SILK_ERROR << "unexpected exception"; + const auto error = make_json_error(request_json, 100, "unexpected exception"); + stream.write_json(error); + } co_await stream.close(); } catch (const std::exception& e) { SILK_ERROR << "exception: " << e.what(); - } catch (...) { - SILK_ERROR << "unexpected exception"; } co_return;