From fa7df8c5e2e4a6bf3be3da940eec1fd577020527 Mon Sep 17 00:00:00 2001 From: Eddy Ashton Date: Thu, 3 Dec 2026 15:09:48 +0000 Subject: [PATCH] Share frontend command exception handling Handle common request exceptions at the shared request boundary rather than duplicating response mapping in the KV and no-KV paths. Keep transaction-specific retry and fatal handling local to KV execution, and cover command error parity across store readiness states. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/node/rpc/frontend.h | 128 ++++++++++------------------ src/node/rpc/test/frontend_test.cpp | 91 ++++++++++++++++++++ 2 files changed, 138 insertions(+), 81 deletions(-) diff --git a/src/node/rpc/frontend.h b/src/node/rpc/frontend.h index 7755ceb5f6c..fd3c01ce90d 100644 --- a/src/node/rpc/frontend.h +++ b/src/node/rpc/frontend.h @@ -626,7 +626,35 @@ namespace ccf const auto start_time = std::chrono::high_resolution_clock::now(); - process_command_inner(ctx, endpoint, attempts); + try + { + process_command_inner(ctx, endpoint, attempts); + } + catch (RpcException& e) + { + ctx->clear_response_headers(); + ctx->set_error(std::move(e.error)); + } + catch (const ccf::JsonParseError& e) + { + ctx->clear_response_headers(); + ctx->set_error( + HTTP_STATUS_BAD_REQUEST, ccf::errors::InvalidInput, e.describe()); + } + catch (const nlohmann::json::exception& e) + { + ctx->clear_response_headers(); + ctx->set_error( + HTTP_STATUS_BAD_REQUEST, ccf::errors::InvalidInput, e.what()); + } + catch (const std::exception& e) + { + ctx->clear_response_headers(); + ctx->set_error( + HTTP_STATUS_INTERNAL_SERVER_ERROR, + ccf::errors::InternalError, + e.what()); + } const auto end_time = std::chrono::high_resolution_clock::now(); @@ -661,58 +689,29 @@ namespace ccf endpoints::EndpointDefinitionPtr& endpoint, size_t& attempts) { - try - { - endpoint = endpoints.find_endpoint_without_kv(*ctx); - if ( - endpoint == nullptr || - endpoint->execution_mode != - endpoints::EndpointExecutionMode::Command || - !endpoint->authn_policies.empty() || - endpoint->properties.forwarding_required != - endpoints::ForwardingRequired::Never) - { - ctx->set_error( - HTTP_STATUS_SERVICE_UNAVAILABLE, - ccf::errors::FrontendNotOpen, - "KV store is not ready."); - return; - } - - if (!check_uri_allowed(ctx, endpoint)) - { - return; - } - - ++attempts; - endpoints::CommandEndpointContext args(ctx); - endpoints.execute_command_endpoint(endpoint, args); - } - catch (RpcException& e) - { - ctx->clear_response_headers(); - ctx->set_error(std::move(e.error)); - } - catch (const ccf::JsonParseError& e) - { - ctx->clear_response_headers(); - ctx->set_error( - HTTP_STATUS_BAD_REQUEST, ccf::errors::InvalidInput, e.describe()); - } - catch (const nlohmann::json::exception& e) + endpoint = endpoints.find_endpoint_without_kv(*ctx); + if ( + endpoint == nullptr || + endpoint->execution_mode != endpoints::EndpointExecutionMode::Command || + !endpoint->authn_policies.empty() || + endpoint->properties.forwarding_required != + endpoints::ForwardingRequired::Never) { - ctx->clear_response_headers(); ctx->set_error( - HTTP_STATUS_BAD_REQUEST, ccf::errors::InvalidInput, e.what()); + HTTP_STATUS_SERVICE_UNAVAILABLE, + ccf::errors::FrontendNotOpen, + "KV store is not ready."); + return; } - catch (const std::exception& e) + + if (!check_uri_allowed(ctx, endpoint)) { - ctx->clear_response_headers(); - ctx->set_error( - HTTP_STATUS_INTERNAL_SERVER_ERROR, - ccf::errors::InternalError, - e.what()); + return; } + + ++attempts; + endpoints::CommandEndpointContext args(ctx); + endpoints.execute_command_endpoint(endpoint, args); } bool check_backpressure( @@ -1056,29 +1055,6 @@ namespace ccf "Transaction execution conflicted with compaction: {}", e.what()); continue; } - catch (RpcException& e) - { - ctx->clear_response_headers(); - ctx->set_error(std::move(e.error)); - - return; - } - catch (const ccf::JsonParseError& e) - { - ctx->clear_response_headers(); - ctx->set_error( - HTTP_STATUS_BAD_REQUEST, ccf::errors::InvalidInput, e.describe()); - - return; - } - catch (const nlohmann::json::exception& e) - { - ctx->clear_response_headers(); - ctx->set_error( - HTTP_STATUS_BAD_REQUEST, ccf::errors::InvalidInput, e.what()); - - return; - } catch (const ccf::kv::MaxTransactionSizeExceeded& e) { // Thrown before the transaction is applied, so the store is @@ -1102,16 +1078,6 @@ namespace ccf "Failed to serialise committed transaction"); abort(); } - catch (const std::exception& e) - { - ctx->clear_response_headers(); - ctx->set_error( - HTTP_STATUS_INTERNAL_SERVER_ERROR, - ccf::errors::InternalError, - e.what()); - - return; - } } // end of while loop ctx->clear_response_headers(); diff --git a/src/node/rpc/test/frontend_test.cpp b/src/node/rpc/test/frontend_test.cpp index 550b4463a23..973b17a0592 100644 --- a/src/node/rpc/test/frontend_test.cpp +++ b/src/node/rpc/test/frontend_test.cpp @@ -1576,6 +1576,97 @@ TEST_CASE("KV readiness gate") CHECK(call("/read_only", HTTP_GET).status == HTTP_STATUS_OK); } +TEST_CASE("Command errors are handled consistently with and without KV") +{ + struct RecordingRegistry : public UserEndpointRegistry + { + using UserEndpointRegistry::UserEndpointRegistry; + std::vector completed; + + void handle_event_request_completed( + const ccf::endpoints::RequestCompletedEvent& event) override + { + completed.push_back(event); + } + }; + + std::function fail; + auto expected_status = HTTP_STATUS_BAD_REQUEST; + std::string expected_code = ccf::errors::InvalidInput; + std::string expected_message; + + SUBCASE("RPC exception") + { + fail = []() { + throw ccf::RpcException( + HTTP_STATUS_CONFLICT, "TestError", "Command rejected"); + }; + expected_status = HTTP_STATUS_CONFLICT; + expected_code = "TestError"; + expected_message = "Command rejected"; + } + SUBCASE("CCF JSON exception") + { + fail = []() { throw ccf::JsonParseError("Invalid command"); }; + expected_message = "At #/: Invalid command"; + } + SUBCASE("JSON library exception") + { + fail = []() { (void)nlohmann::json::parse("{"); }; + expected_message = "[json.exception.parse_error."; + } + SUBCASE("Standard exception") + { + fail = []() { throw std::runtime_error("Command failed"); }; + expected_status = HTTP_STATUS_INTERNAL_SERVER_ERROR; + expected_code = ccf::errors::InternalError; + expected_message = "Command failed"; + } + + NetworkState network; + prepare_callers(network); + ccf::StubNodeContext context; + RecordingRegistry registry(context); + RpcFrontend frontend(*network.tables, registry, context); + frontend.open(); + + size_t executions = 0; + registry + .make_command_endpoint( + "/fail", + HTTP_POST, + [&](auto& ctx) { + ++executions; + ctx.rpc_ctx->set_response_header("test-before-error", "true"); + fail(); + }, + no_auth_required) + .set_redirection_strategy(ccf::endpoints::RedirectionStrategy::None) + .install(); + + for (const auto readiness : + {ccf::kv::StoreReadiness::Ready, + ccf::kv::StoreReadiness::Unavailable, + ccf::kv::StoreReadiness::InstallingSnapshot, + ccf::kv::StoreReadiness::Failed}) + { + network.tables->set_readiness(readiness); + auto rpc_ctx = ccf::make_rpc_context( + user_session, create_simple_request("/fail").build_request()); + frontend.process(rpc_ctx); + const auto response = parse_response(rpc_ctx->serialise_response()); + CHECK(response.status == expected_status); + CHECK_FALSE(response.headers.contains("test-before-error")); + const auto error = nlohmann::json::parse(response.body)["error"]; + INFO(error.dump()); + CHECK(error["code"] == expected_code); + CHECK(error["message"].get().starts_with(expected_message)); + REQUIRE(registry.completed.size() == executions); + CHECK(registry.completed.back().status == expected_status); + CHECK(registry.completed.back().attempts == 1); + } +} + TEST_CASE("Templated paths") { NetworkState network;