From 9da3a3cf5095b6ff79989c4fbb127f5508d96097 Mon Sep 17 00:00:00 2001 From: abir Date: Thu, 20 Jul 2023 14:53:15 +0400 Subject: [PATCH] fix: handle route not found --- packages/app/src/Application.ts | 2 +- packages/ws/src/WebSocketServer.ts | 19 ++++++++++++++----- yarn.lock | 4 ++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/app/src/Application.ts b/packages/app/src/Application.ts index c80c0a7c..4f109fee 100644 --- a/packages/app/src/Application.ts +++ b/packages/app/src/Application.ts @@ -100,7 +100,7 @@ export class Application { return this.#index; } - protected captureException(reason: unknown): void { + public captureException(reason: unknown): void { this.logger.error("Unexpected error", reason); const {error} = this.#unit.getTransactionHandlers(); diff --git a/packages/ws/src/WebSocketServer.ts b/packages/ws/src/WebSocketServer.ts index 2d42c6dc..166d6f87 100644 --- a/packages/ws/src/WebSocketServer.ts +++ b/packages/ws/src/WebSocketServer.ts @@ -15,7 +15,7 @@ import { unit, Unit, } from "@bunt/unit"; -import {assert, Defer, isDefined, isString, logger, Logger, noop, resolveOrReject, toError} from "@bunt/util"; +import {assert, Defer, isDefined, isString, logger, Logger, noop, resolveOrReject} from "@bunt/util"; import {RequestMessage, WebServer} from "@bunt/web"; import * as ws from "ws"; import {WebSocketCloseReason} from "./const.js"; @@ -91,7 +91,7 @@ export class WebSocketServer extends Disposer implements IRun webSocket.close(resolveOrReject(resolve, reject)); })); } catch (error) { - this.logger.error("Unexpected error", error); + this.#web.captureException(error); } } @@ -226,8 +226,17 @@ export class WebSocketServer extends Disposer implements IRun this.handle(connection, () => this.#unit.run(route.action as any, state)); }); } catch (error) { - this.logger.error(toError(error).message, error); - socket.destroy(toError(error)); + this.#web.captureException(error); + + const response = [ + "HTTP/1.1 404 Not found", + "Content-Type: text/plain", + "Content-Length: 0", + "Connection: close", + "", + ]; + + socket.end(response.join("\r\n")); } }; @@ -236,7 +245,7 @@ export class WebSocketServer extends Disposer implements IRun await action(); connection.close(WebSocketCloseReason.NORMAL_CLOSURE); } catch (error) { - this.logger.error("Unexpected error", error); + this.#web.captureException(error); connection.close(WebSocketCloseReason.INTERNAL_ERROR); } } diff --git a/yarn.lock b/yarn.lock index a5e1eec1..43739846 100644 --- a/yarn.lock +++ b/yarn.lock @@ -551,7 +551,7 @@ __metadata: languageName: unknown linkType: soft -"@bunt/web@^0.29.2, @bunt/web@workspace:packages/web": +"@bunt/web@^0.29.3, @bunt/web@workspace:packages/web": version: 0.0.0-use.local resolution: "@bunt/web@workspace:packages/web" dependencies: @@ -569,7 +569,7 @@ __metadata: "@bunt/app": ^0.29.2 "@bunt/unit": ^0.29.0 "@bunt/util": ^0.29.0 - "@bunt/web": ^0.29.2 + "@bunt/web": ^0.29.3 "@types/websocket": ^1.0.5 "@types/ws": ^8.5.5 websocket: ^1.0.34