|
1 | 1 | import { logger } from "@coder/logger"
|
2 | 2 | import bodyParser from "body-parser"
|
3 | 3 | import cookieParser from "cookie-parser"
|
4 |
| -import { Express } from "express" |
| 4 | +import { ErrorRequestHandler, Express } from "express" |
5 | 5 | import { promises as fs } from "fs"
|
6 | 6 | import http from "http"
|
7 | 7 | import * as path from "path"
|
@@ -100,24 +100,25 @@ export const register = async (app: Express, server: http.Server, args: Defaulte
|
100 | 100 | throw new HttpError("Not Found", HttpCode.NotFound)
|
101 | 101 | })
|
102 | 102 |
|
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) => { |
106 | 104 | const resourcePath = path.resolve(rootPath, "src/browser/pages/error.html")
|
107 | 105 | res.set("Content-Type", getMediaMime(resourcePath))
|
108 | 106 | try {
|
109 | 107 | const content = await fs.readFile(resourcePath, "utf8")
|
110 | 108 | if (err.code === "ENOENT" || err.code === "EISDIR") {
|
111 | 109 | err.status = HttpCode.NotFound
|
112 | 110 | }
|
113 |
| - res.status(err.status || 500).send( |
| 111 | + const status = err.status ?? err.statusCode ?? 500 |
| 112 | + res.status(status).send( |
114 | 113 | 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) |
117 | 116 | .replace(/{{ERROR_BODY}}/g, err.message),
|
118 | 117 | )
|
119 | 118 | } catch (error) {
|
120 | 119 | next(error)
|
121 | 120 | }
|
122 |
| - }) |
| 121 | + } |
| 122 | + |
| 123 | + app.use(errorHandler) |
123 | 124 | }
|
0 commit comments