Skip to content

Commit

Permalink
fix: handle route not found
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Jul 20, 2023
1 parent a85e514 commit 9da3a3c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/app/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class Application<C extends Context> {
return this.#index;
}

protected captureException(reason: unknown): void {
public captureException(reason: unknown): void {
this.logger.error("Unexpected error", reason);

const {error} = this.#unit.getTransactionHandlers();
Expand Down
19 changes: 14 additions & 5 deletions packages/ws/src/WebSocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -91,7 +91,7 @@ export class WebSocketServer<C extends Context> extends Disposer implements IRun
webSocket.close(resolveOrReject(resolve, reject));
}));
} catch (error) {
this.logger.error("Unexpected error", error);
this.#web.captureException(error);
}
}

Expand Down Expand Up @@ -226,8 +226,17 @@ export class WebSocketServer<C extends Context> 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"));
}
};

Expand All @@ -236,7 +245,7 @@ export class WebSocketServer<C extends Context> 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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 9da3a3c

Please sign in to comment.