Skip to content

Commit 6ab6cb4

Browse files
committed
Fix error handler types
1 parent 6422a8d commit 6ab6cb4

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/node/routes/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { logger } from "@coder/logger"
22
import bodyParser from "body-parser"
33
import cookieParser from "cookie-parser"
4-
import { Express } from "express"
4+
import { ErrorRequestHandler, Express } from "express"
55
import { promises as fs } from "fs"
66
import http from "http"
77
import * as path from "path"
@@ -100,24 +100,25 @@ export const register = async (app: Express, server: http.Server, args: Defaulte
100100
throw new HttpError("Not Found", HttpCode.NotFound)
101101
})
102102

103-
// Handle errors.
104-
// TODO: The types are broken; says they're all implicitly `any`.
105-
app.use(async (err: any, req: any, res: any, next: any) => {
103+
const errorHandler: ErrorRequestHandler = async (err, req, res, next) => {
106104
const resourcePath = path.resolve(rootPath, "src/browser/pages/error.html")
107105
res.set("Content-Type", getMediaMime(resourcePath))
108106
try {
109107
const content = await fs.readFile(resourcePath, "utf8")
110108
if (err.code === "ENOENT" || err.code === "EISDIR") {
111109
err.status = HttpCode.NotFound
112110
}
113-
res.status(err.status || 500).send(
111+
const status = err.status ?? err.statusCode ?? 500
112+
res.status(status).send(
114113
replaceTemplates(req, content)
115-
.replace(/{{ERROR_TITLE}}/g, err.status || "Error")
116-
.replace(/{{ERROR_HEADER}}/g, err.status || "Error")
114+
.replace(/{{ERROR_TITLE}}/g, status)
115+
.replace(/{{ERROR_HEADER}}/g, status)
117116
.replace(/{{ERROR_BODY}}/g, err.message),
118117
)
119118
} catch (error) {
120119
next(error)
121120
}
122-
})
121+
}
122+
123+
app.use(errorHandler)
123124
}

0 commit comments

Comments
 (0)