-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
errors.ts
38 lines (34 loc) · 1012 Bytes
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */
import { ErrorRequestHandler } from "express";
import { isHttpError } from "http-errors";
import { reportError } from "../core";
/**
* Renders an error page.
*/
export const handleError: ErrorRequestHandler = function (
err,
req,
res,
next // eslint-disable-line @typescript-eslint/no-unused-vars
) {
reportError(err, req);
const statusCode = isHttpError(err) ? err.statusCode : 500;
res.status(statusCode);
if (/application\/json;/.test(req.get("accept") ?? "")) {
res.send(err);
} else if (statusCode === 404) {
res.render("error", {
title: "Page Not Found",
message: "Sorry, but the page you were trying to view does not exist.",
layout: false,
});
} else {
res.render("error", {
title: "Application Error",
message: "An error occurred while processing this request.",
stack: err.stack,
layout: false,
});
}
};