Skip to content

Commit abe5af5

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 abe5af5

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
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

+7-7
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,15 @@ 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 {
136+
if (req.accepts("text/html")) {
142137
const resourcePath = path.resolve(rootPath, "src/browser/pages/error.html")
143138
res.set("Content-Type", getMediaMime(resourcePath))
144139
const content = await fs.readFile(resourcePath, "utf8")
@@ -148,6 +143,11 @@ export const register = async (
148143
.replace(/{{ERROR_HEADER}}/g, status)
149144
.replace(/{{ERROR_BODY}}/g, err.message),
150145
)
146+
} else {
147+
res.json({
148+
error: err.message,
149+
...(err.details || {}),
150+
})
151151
}
152152
}
153153

0 commit comments

Comments
 (0)