Skip to content

Commit 6f601e9

Browse files
committed
feat: add final error handlers for HTTP and WebSocket with detailed documentation
1 parent b1eb2b7 commit 6f601e9

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/node/routes/errors.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export const errorHasCode = (error: any): error is ErrorWithCode => {
2828

2929
const notFoundCodes = [404, "ENOENT", "EISDIR"]
3030

31+
/**
32+
* Final HTTP error handler.
33+
*
34+
* Note: This handler intentionally does not call `next()` even though it
35+
* accepts it as an argument; it is expected to be mounted last.
36+
*/
3137
export const errorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
3238
let statusCode = 500
3339

@@ -61,6 +67,12 @@ export const errorHandler: express.ErrorRequestHandler = async (err, req, res, n
6167
}
6268
}
6369

70+
/**
71+
* Final WebSocket error handler.
72+
*
73+
* Note: This handler intentionally does not call `next()` even though it
74+
* accepts it as an argument; it is expected to be mounted last.
75+
*/
6476
export const wsErrorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
6577
let statusCode = 500
6678
if (errorHasStatusCode(err)) {
@@ -73,8 +85,5 @@ export const wsErrorHandler: express.ErrorRequestHandler = async (err, req, res,
7385
} else {
7486
logger.debug(`${err.message} ${err.stack}`)
7587
}
76-
// Close the WebSocket connection with the appropriate HTTP status code.
77-
// We don't call next() here because the error has been fully handled:
78-
// the connection is closed and the error has been logged.
7988
;(req as WebsocketRequest).ws.end(`HTTP/1.1 ${statusCode} ${err.message}\r\n\r\n`)
8089
}

0 commit comments

Comments
 (0)