Skip to content

Commit 504b164

Browse files
committed
node/routes: Fix error handling
We should always send HTML if the user agent expects it. If they do not, they should clearly indicate such via the Accept header. Closes #2297
1 parent 5e60305 commit 504b164

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

src/node/routes/domainProxy.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ router.all("*", (req, res, next) => {
4545
return next()
4646
}
4747

48-
// Assume anything that explicitly accepts text/html is a user browsing a
49-
// page (as opposed to an xhr request). Don't use `req.accepts()` since
50-
// *every* request that I've seen (in Firefox and Chromium at least)
51-
// includes `*/*` making it always truthy.
52-
if (typeof req.headers.accepts === "string" && req.headers.accepts.split(",").includes("text/html")) {
48+
if (req.accepts("text/html")) {
5349
// Let the login through.
5450
if (/\/login\/?/.test(req.path)) {
5551
return next()

src/node/routes/index.ts

+10-17
Original file line numberDiff line numberDiff line change
@@ -125,30 +125,23 @@ export const register = async (
125125
throw new HttpError("Not Found", HttpCode.NotFound)
126126
})
127127

128-
const errorHandler: express.ErrorRequestHandler = async (err, req, res) => {
128+
const errorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
129129
if (err.code === "ENOENT" || err.code === "EISDIR") {
130130
err.status = HttpCode.NotFound
131131
}
132132

133133
const status = err.status ?? err.statusCode ?? 500
134134
res.status(status)
135135

136-
if (req.accepts("application/json")) {
137-
res.json({
138-
error: err.message,
139-
...(err.details || {}),
140-
})
141-
} else {
142-
const resourcePath = path.resolve(rootPath, "src/browser/pages/error.html")
143-
res.set("Content-Type", getMediaMime(resourcePath))
144-
const content = await fs.readFile(resourcePath, "utf8")
145-
res.send(
146-
replaceTemplates(req, content)
147-
.replace(/{{ERROR_TITLE}}/g, status)
148-
.replace(/{{ERROR_HEADER}}/g, status)
149-
.replace(/{{ERROR_BODY}}/g, err.message),
150-
)
151-
}
136+
const resourcePath = path.resolve(rootPath, "src/browser/pages/error.html")
137+
res.set("Content-Type", getMediaMime(resourcePath))
138+
const content = await fs.readFile(resourcePath, "utf8")
139+
res.send(
140+
replaceTemplates(req, content)
141+
.replace(/{{ERROR_TITLE}}/g, status)
142+
.replace(/{{ERROR_HEADER}}/g, status)
143+
.replace(/{{ERROR_BODY}}/g, err.message),
144+
)
152145
}
153146

154147
app.use(errorHandler)

0 commit comments

Comments
 (0)