1
0
mirror of https://git.tuxpa.in/a/code-server.git synced 2024-12-27 20:55:25 +00:00

Fix error handler types

This commit is contained in:
Asher 2020-10-27 17:18:44 -05:00
parent 6422a8d74b
commit 6ab6cb4f07
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A

View File

@ -1,7 +1,7 @@
import { logger } from "@coder/logger" import { logger } from "@coder/logger"
import bodyParser from "body-parser" import bodyParser from "body-parser"
import cookieParser from "cookie-parser" import cookieParser from "cookie-parser"
import { Express } from "express" import { ErrorRequestHandler, Express } from "express"
import { promises as fs } from "fs" import { promises as fs } from "fs"
import http from "http" import http from "http"
import * as path from "path" import * as path from "path"
@ -100,9 +100,7 @@ export const register = async (app: Express, server: http.Server, args: Defaulte
throw new HttpError("Not Found", HttpCode.NotFound) throw new HttpError("Not Found", HttpCode.NotFound)
}) })
// Handle errors. const errorHandler: ErrorRequestHandler = async (err, req, res, next) => {
// TODO: The types are broken; says they're all implicitly `any`.
app.use(async (err: any, req: any, res: any, next: any) => {
const resourcePath = path.resolve(rootPath, "src/browser/pages/error.html") const resourcePath = path.resolve(rootPath, "src/browser/pages/error.html")
res.set("Content-Type", getMediaMime(resourcePath)) res.set("Content-Type", getMediaMime(resourcePath))
try { try {
@ -110,14 +108,17 @@ export const register = async (app: Express, server: http.Server, args: Defaulte
if (err.code === "ENOENT" || err.code === "EISDIR") { if (err.code === "ENOENT" || err.code === "EISDIR") {
err.status = HttpCode.NotFound err.status = HttpCode.NotFound
} }
res.status(err.status || 500).send( const status = err.status ?? err.statusCode ?? 500
res.status(status).send(
replaceTemplates(req, content) replaceTemplates(req, content)
.replace(/{{ERROR_TITLE}}/g, err.status || "Error") .replace(/{{ERROR_TITLE}}/g, status)
.replace(/{{ERROR_HEADER}}/g, err.status || "Error") .replace(/{{ERROR_HEADER}}/g, status)
.replace(/{{ERROR_BODY}}/g, err.message), .replace(/{{ERROR_BODY}}/g, err.message),
) )
} catch (error) { } catch (error) {
next(error) next(error)
} }
}) }
app.use(errorHandler)
} }