Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 37 additions & 77 deletions src/node/rpc/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,43 @@ 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 (const ccf::kv::MapAccessDenied& e)
{
ctx->clear_response_headers();
ctx->set_error(
HTTP_STATUS_INTERNAL_SERVER_ERROR,
ccf::errors::InternalError,
e.what());
}
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();

Expand Down Expand Up @@ -698,39 +734,6 @@ namespace ccf
"Command execution conflicted with compaction: {}", e.what());
return false;
}
catch (const ccf::kv::MapAccessDenied& e)
{
ctx->clear_response_headers();
ctx->set_error(
HTTP_STATUS_INTERNAL_SERVER_ERROR,
ccf::errors::InternalError,
e.what());
}
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());
}

return true;
}
Expand Down Expand Up @@ -1079,39 +1082,6 @@ namespace ccf
"Transaction execution conflicted with compaction: {}", e.what());
continue;
}
catch (const ccf::kv::MapAccessDenied& e)
{
ctx->clear_response_headers();
ctx->set_error(
HTTP_STATUS_INTERNAL_SERVER_ERROR,
ccf::errors::InternalError,
e.what());

return;
}
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
Expand All @@ -1135,16 +1105,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();
Expand Down
98 changes: 98 additions & 0 deletions src/node/rpc/test/frontend_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,104 @@ 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<ccf::endpoints::RequestCompletedEvent> completed;

void handle_event_request_completed(
const ccf::endpoints::RequestCompletedEvent& event) override
{
completed.push_back(event);
}
};

std::function<void()> fail;
auto expected_status = HTTP_STATUS_BAD_REQUEST;
std::string expected_code = ccf::errors::InvalidInput;
std::string expected_message;

SUBCASE("Map access denied")
{
fail = []() { throw ccf::kv::MapAccessDenied("Command access denied"); };
expected_status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
expected_code = ccf::errors::InternalError;
expected_message = "Command access denied";
}
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<std::string>().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;
Expand Down