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
This commit is contained in:
parent
7afa689285
commit
40a7c11ce3
|
@ -48,8 +48,8 @@ router.all("*", (req, res, next) => {
|
|||
// Assume anything that explicitly accepts text/html is a user browsing a
|
||||
// page (as opposed to an xhr request). Don't use `req.accepts()` since
|
||||
// *every* request that I've seen (in Firefox and Chromium at least)
|
||||
// includes `*/*` making it always truthy.
|
||||
if (typeof req.headers.accepts === "string" && req.headers.accepts.split(",").includes("text/html")) {
|
||||
// includes `*/*` making it always truthy. Even for css/javascript.
|
||||
if (req.headers.accept && req.headers.accept.includes("text/html")) {
|
||||
// Let the login through.
|
||||
if (/\/login\/?/.test(req.path)) {
|
||||
return next()
|
||||
|
|
|
@ -125,7 +125,7 @@ export const register = async (
|
|||
throw new HttpError("Not Found", HttpCode.NotFound)
|
||||
})
|
||||
|
||||
const errorHandler: express.ErrorRequestHandler = async (err, req, res) => {
|
||||
const errorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
|
||||
if (err.code === "ENOENT" || err.code === "EISDIR") {
|
||||
err.status = HttpCode.NotFound
|
||||
}
|
||||
|
@ -133,12 +133,11 @@ export const register = async (
|
|||
const status = err.status ?? err.statusCode ?? 500
|
||||
res.status(status)
|
||||
|
||||
if (req.accepts("application/json")) {
|
||||
res.json({
|
||||
error: err.message,
|
||||
...(err.details || {}),
|
||||
})
|
||||
} else {
|
||||
// Assume anything that explicitly accepts text/html is a user browsing a
|
||||
// page (as opposed to an xhr request). Don't use `req.accepts()` since
|
||||
// *every* request that I've seen (in Firefox and Chromium at least)
|
||||
// includes `*/*` making it always truthy. Even for css/javascript.
|
||||
if (req.headers.accept && req.headers.accept.includes("text/html")) {
|
||||
const resourcePath = path.resolve(rootPath, "src/browser/pages/error.html")
|
||||
res.set("Content-Type", getMediaMime(resourcePath))
|
||||
const content = await fs.readFile(resourcePath, "utf8")
|
||||
|
@ -148,6 +147,11 @@ export const register = async (
|
|||
.replace(/{{ERROR_HEADER}}/g, status)
|
||||
.replace(/{{ERROR_BODY}}/g, err.message),
|
||||
)
|
||||
} else {
|
||||
res.json({
|
||||
error: err.message,
|
||||
...(err.details || {}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue