From c1b796c6056e29399d1caefef1f7c4632bcc4789 Mon Sep 17 00:00:00 2001 From: Nathan Brahms Date: Fri, 20 Sep 2024 11:59:49 -0700 Subject: [PATCH] Improve error reporting to server consumers (#29) * Improve error reporting to server consumers Previously we emmitted error codes without any error data. Instead, actually transmit received errors to the Braekhus server consumer. * Also: update test ports to avoid conflicts in the local system --- client/index.ts | 2 +- log/index.ts | 1 + server/__tests__/e2e.test.ts | 10 +++++----- server/proxy.ts | 6 +++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/client/index.ts b/client/index.ts index 44c8194..d4953ae 100644 --- a/client/index.ts +++ b/client/index.ts @@ -4,7 +4,7 @@ import { JSONRPCServer, JSONRPCServerAndClient, } from "json-rpc-2.0"; -import { isArray, omit } from "lodash"; +import { omit } from "lodash"; import { Logger } from "pino"; import WebSocket from "ws"; diff --git a/log/index.ts b/log/index.ts index 7a11bfe..6eece86 100644 --- a/log/index.ts +++ b/log/index.ts @@ -13,6 +13,7 @@ export const createLogger = ( ): Logger => { const logger = pinoLogger({ ...options, + name: `braekhus.${options.name}`, level: process.env.LOG_LEVEL || "info", serializers: { error: serializeError, diff --git a/server/__tests__/e2e.test.ts b/server/__tests__/e2e.test.ts index 3f394af..5df77eb 100644 --- a/server/__tests__/e2e.test.ts +++ b/server/__tests__/e2e.test.ts @@ -7,9 +7,9 @@ import { JsonRpcClient } from "../../client"; import { Backoff } from "../../client/backoff"; import { testHttpServer } from "../testing/testExpressApp"; -const SERVER_RPC_PORT = 8080; -const SERVER_PROXY_PORT = 8081; -const TARGET_PORT = 8082; +const SERVER_RPC_PORT = 18080; +const SERVER_PROXY_PORT = 18081; +const TARGET_PORT = 18082; type Client = { jsonRpcClient: JsonRpcClient; @@ -78,7 +78,7 @@ describe("Proxy server starts up first", () => { ).resolves.toMatchObject( expect.objectContaining({ status: 502, - text: "Bad Gateway", + text: '{"error":{"type":"channel_not_found"},"message":"Error: Channel not found: testChannelId"}', }) ); }); @@ -89,7 +89,7 @@ describe("Proxy server starts up first", () => { ).resolves.toMatchObject( expect.objectContaining({ status: 502, - text: "Bad Gateway", + text: '{"error":{},"message":"Error: Client not found: noSuchClientId"}', }) ); }); diff --git a/server/proxy.ts b/server/proxy.ts index eceb17c..517a15b 100644 --- a/server/proxy.ts +++ b/server/proxy.ts @@ -40,7 +40,7 @@ export const httpProxyApp = ( const matches = PATH_REGEXP.exec(req.path); logger.debug({ matches }, "path matches"); if (matches === null || matches.length !== 3) { - res.sendStatus(400); + res.status(404).json({ error: "Invalid path format" }); } const clientId = (matches as string[])[1]; @@ -89,9 +89,9 @@ export const httpProxyApp = ( (error.message.startsWith("timeout") && error.message.endsWith("exceeded")) ) { - res.sendStatus(504); + res.status(504).json({ error, message: String(error) }); } else { - res.sendStatus(502); + res.status(502).json({ error, message: String(error) }); } } });