Skip to content

Commit 1a1d131

Browse files
committed
Add type for websocket error handler
1 parent d67bcab commit 1a1d131

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/node/routes/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { redirect, replaceTemplates } from "../http"
1616
import { PluginAPI } from "../plugin"
1717
import { getMediaMime, paths } from "../util"
1818
import { wrapper } from "../wrapper"
19+
import { WebsocketErrorRequestHandler } from "../wsRouter"
1920
import * as apps from "./apps"
2021
import * as domainProxy from "./domainProxy"
2122
import * as health from "./health"
@@ -180,9 +181,9 @@ export const register = async (
180181

181182
app.use(errorHandler)
182183

183-
const wsErrorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
184+
const wsErrorHandler: WebsocketErrorRequestHandler = async (err, req, res, next) => {
184185
logger.error(`${err.message} ${err.stack}`)
185-
;(req as pluginapi.WebsocketRequest).ws.end()
186+
req.ws.end()
186187
}
187188

188189
wsApp.use(wsErrorHandler)

src/node/wsRouter.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ export const handleUpgrade = (app: express.Express, server: http.Server): void =
2121
})
2222
}
2323

24+
// This is a version of expressCore.ErrorRequestHandler with the websocket on
25+
// the request.
26+
export type WebsocketErrorRequestHandler<
27+
P = expressCore.ParamsDictionary,
28+
ResBody = any,
29+
ReqBody = any,
30+
ReqQuery = expressCore.Query
31+
> = (
32+
err: any,
33+
req: pluginapi.WebsocketRequest<P, ResBody, ReqBody, ReqQuery>,
34+
res: Response<ResBody>,
35+
next: express.Core.NextFunction,
36+
) => any
37+
2438
interface InternalWebsocketRequest extends pluginapi.WebsocketRequest {
2539
_ws_handled: boolean
2640
}

0 commit comments

Comments
 (0)