From cb9e4f14e8c9169d5f603e9af3aff74c79799312 Mon Sep 17 00:00:00 2001 From: thunkar Date: Tue, 7 Jan 2025 11:02:19 +0000 Subject: [PATCH 1/3] optionally handle rpc errors with 200 + err body --- noir-projects/Earthfile | 2 +- .../json-rpc/server/safe_json_rpc_server.ts | 23 +++++++++++++++---- yarn-project/txe/src/index.ts | 2 +- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/noir-projects/Earthfile b/noir-projects/Earthfile index afa91d7e473..d0d9242c57b 100644 --- a/noir-projects/Earthfile +++ b/noir-projects/Earthfile @@ -2,7 +2,7 @@ VERSION 0.8 test: BUILD +test-protocol-circuits BUILD +test-aztec-nr - # BUILD +test-contracts + BUILD +test-contracts test-protocol-circuits: FROM ../+bootstrap diff --git a/yarn-project/foundation/src/json-rpc/server/safe_json_rpc_server.ts b/yarn-project/foundation/src/json-rpc/server/safe_json_rpc_server.ts index 54436d63efa..8d680cada32 100644 --- a/yarn-project/foundation/src/json-rpc/server/safe_json_rpc_server.ts +++ b/yarn-project/foundation/src/json-rpc/server/safe_json_rpc_server.ts @@ -24,6 +24,11 @@ export class SafeJsonRpcServer { constructor( /** The proxy object to delegate requests to. */ private readonly proxy: Proxy, + /** + * Return an HTTP 200 status code on errors, but include an error object + * as per the JSON RPC spec + */ + private http200OnError = false, /** Health check function */ private readonly healthCheck: StatusCheckFn = () => true, /** Logger */ @@ -105,9 +110,17 @@ export class SafeJsonRpcServer { ctx.status = 400; ctx.body = { jsonrpc, id, error: { code: -32601, message: `Method not found: ${method}` } }; } else { - const result = await this.proxy.call(method, params); - ctx.body = { jsonrpc, id, result }; ctx.status = 200; + try { + const result = await this.proxy.call(method, params); + ctx.body = { jsonrpc, id, result }; + } catch (err: any) { + if (this.http200OnError) { + ctx.body = { jsonrpc, id, error: { code: err.code || -32600, data: err.data, message: err.message } }; + } else { + throw err; + } + } } }); @@ -259,20 +272,22 @@ function makeAggregateHealthcheck(namedHandlers: NamespacedApiHandlers, log?: Lo */ export function createNamespacedSafeJsonRpcServer( handlers: NamespacedApiHandlers, + http200OnError = false, log = createLogger('json-rpc:server'), ): SafeJsonRpcServer { const proxy = new NamespacedSafeJsonProxy(handlers); const healthCheck = makeAggregateHealthcheck(handlers, log); - return new SafeJsonRpcServer(proxy, healthCheck, log); + return new SafeJsonRpcServer(proxy, http200OnError, healthCheck, log); } export function createSafeJsonRpcServer( handler: T, schema: ApiSchemaFor, + http200OnError = false, healthCheck?: StatusCheckFn, ) { const proxy = new SafeJsonProxy(handler, schema); - return new SafeJsonRpcServer(proxy, healthCheck); + return new SafeJsonRpcServer(proxy, http200OnError, healthCheck); } /** diff --git a/yarn-project/txe/src/index.ts b/yarn-project/txe/src/index.ts index 9ba2b8d7b5c..1fab653ae0a 100644 --- a/yarn-project/txe/src/index.ts +++ b/yarn-project/txe/src/index.ts @@ -120,5 +120,5 @@ const TXEDispatcherApiSchema: ApiSchemaFor = { * @returns A TXE RPC server. */ export function createTXERpcServer(logger: Logger) { - return createSafeJsonRpcServer(new TXEDispatcher(logger), TXEDispatcherApiSchema); + return createSafeJsonRpcServer(new TXEDispatcher(logger), TXEDispatcherApiSchema, true); } From bbdebd75ec67b42b5e457f0ed18c6cc9270682fc Mon Sep 17 00:00:00 2001 From: thunkar Date: Tue, 7 Jan 2025 11:18:46 +0000 Subject: [PATCH 2/3] fix --- yarn-project/bot/src/rpc.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/bot/src/rpc.ts b/yarn-project/bot/src/rpc.ts index 3fe11e372e1..cca9df44e9c 100644 --- a/yarn-project/bot/src/rpc.ts +++ b/yarn-project/bot/src/rpc.ts @@ -9,7 +9,7 @@ import { type BotRunner } from './runner.js'; * @returns An JSON-RPC HTTP server */ export function createBotRunnerRpcServer(botRunner: BotRunner) { - createSafeJsonRpcServer(botRunner, BotRunnerApiSchema, botRunner.isHealthy.bind(botRunner)); + createSafeJsonRpcServer(botRunner, BotRunnerApiSchema, false, botRunner.isHealthy.bind(botRunner)); } export function getBotRunnerApiHandler(botRunner: BotRunner): ApiHandler { From 0ddc6b12d2d5a9d6ded057bb4ecbdd4bc877e528 Mon Sep 17 00:00:00 2001 From: thunkar Date: Tue, 7 Jan 2025 11:38:41 +0000 Subject: [PATCH 3/3] somehow bootstrap fast didn't catch this --- yarn-project/aztec/src/cli/aztec_start_action.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/aztec/src/cli/aztec_start_action.ts b/yarn-project/aztec/src/cli/aztec_start_action.ts index 0e45a891463..c45892eccc1 100644 --- a/yarn-project/aztec/src/cli/aztec_start_action.ts +++ b/yarn-project/aztec/src/cli/aztec_start_action.ts @@ -102,7 +102,7 @@ export async function aztecStart(options: any, userLog: LogFn, debugLogger: Logg installSignalHandlers(debugLogger.info, signalHandlers); if (Object.entries(services).length > 0) { - const rpcServer = createNamespacedSafeJsonRpcServer(services, debugLogger); + const rpcServer = createNamespacedSafeJsonRpcServer(services, false, debugLogger); const { port } = await startHttpRpcServer(rpcServer, { port: options.port }); debugLogger.info(`Aztec Server listening on port ${port}`); }