Improve error handling
This commit is contained in:
@@ -14,10 +14,7 @@ let errorHtmlCache: Buffer | undefined;
|
||||
|
||||
const readErrorFile = (): Buffer => {
|
||||
if (isDev) {
|
||||
return (
|
||||
errorHtmlCache ??
|
||||
(errorHtmlCache = fs.readFileSync(path.join(__dirname, "error.dev.html")))
|
||||
);
|
||||
return fs.readFileSync(path.join(__dirname, "error.dev.html"));
|
||||
}
|
||||
|
||||
if (isProd) {
|
||||
@@ -82,10 +79,27 @@ export default function onerror(app: Koa) {
|
||||
err = newError;
|
||||
}
|
||||
|
||||
if (err.code === "ENOENT") {
|
||||
if (err instanceof ValidationError) {
|
||||
// @ts-expect-error status is not a property on ValidationError
|
||||
err.status = 400;
|
||||
|
||||
if (err.errors && err.errors[0]) {
|
||||
err.message = `${err.errors[0].message} (${err.errors[0].path})`;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
err.code === "ENOENT" ||
|
||||
err instanceof EmptyResultError ||
|
||||
/Not found/i.test(err.message)
|
||||
) {
|
||||
err.status = 404;
|
||||
}
|
||||
|
||||
if (/Authorization error/i.test(err.message)) {
|
||||
err.status = 403;
|
||||
}
|
||||
|
||||
if (typeof err.status !== "number" || !http.STATUS_CODES[err.status]) {
|
||||
err.status = 500;
|
||||
}
|
||||
@@ -134,47 +148,37 @@ export default function onerror(app: Koa) {
|
||||
|
||||
function json(err: any, ctx: Context) {
|
||||
ctx.status = err.status;
|
||||
let message = err.message || err.name;
|
||||
let error;
|
||||
let message;
|
||||
let id;
|
||||
|
||||
if (err instanceof ValidationError) {
|
||||
// super basic form error handling
|
||||
ctx.status = 400;
|
||||
|
||||
if (err.errors && err.errors[0]) {
|
||||
message = `${err.errors[0].message} (${err.errors[0].path})`;
|
||||
}
|
||||
if (ctx.status === 400) {
|
||||
message = "Validation error";
|
||||
id = "validation_error";
|
||||
}
|
||||
|
||||
if (err instanceof EmptyResultError || /Not found/i.test(message)) {
|
||||
if (ctx.status === 401) {
|
||||
message = "Authentication error";
|
||||
id = "authentication_error";
|
||||
}
|
||||
if (ctx.status === 403) {
|
||||
message = "Authorization error";
|
||||
id = "authorization_error";
|
||||
}
|
||||
if (ctx.status === 404) {
|
||||
message = "Resource not found";
|
||||
ctx.status = 404;
|
||||
error = "not_found";
|
||||
id = "not_found";
|
||||
}
|
||||
|
||||
if (/Authorization error/i.test(message)) {
|
||||
ctx.status = 403;
|
||||
error = "authorization_error";
|
||||
}
|
||||
|
||||
if (ctx.status === 500) {
|
||||
message = "Internal server error";
|
||||
error = "internal_server_error";
|
||||
id = "internal_server_error";
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
ok: false,
|
||||
error: snakeCase(err.id || error),
|
||||
error: snakeCase(err.id || id),
|
||||
status: ctx.status,
|
||||
message,
|
||||
data: err.errorData,
|
||||
message: err.message || message || err.name,
|
||||
data: err.errorData ?? undefined,
|
||||
};
|
||||
|
||||
// @ts-expect-error ts-migrate(2571) FIXME: Object is of type 'unknown'.
|
||||
if (!ctx.body.data) {
|
||||
// @ts-expect-error ts-migrate(2571) FIXME: Object is of type 'unknown'.
|
||||
delete ctx.body.data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,6 +192,7 @@ function html(err: any, ctx: Context) {
|
||||
const page = readErrorFile();
|
||||
ctx.body = page
|
||||
.toString()
|
||||
.replace(/\/\/inject-message\/\//g, escape(err.message))
|
||||
.replace(/\/\/inject-status\/\//g, escape(err.status))
|
||||
.replace(/\/\/inject-stack\/\//g, escape(err.stack));
|
||||
ctx.type = "html";
|
||||
|
||||
@@ -83,7 +83,7 @@ Object {
|
||||
|
||||
exports[`#groups.update when user is admin fails with validation error when name already taken 1`] = `
|
||||
Object {
|
||||
"error": "",
|
||||
"error": "validation_error",
|
||||
"message": "The name of this group is already in use (isUniqueNameInTeam)",
|
||||
"ok": false,
|
||||
"status": 400,
|
||||
|
||||
@@ -2,33 +2,44 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Error - //inject-status//</title>
|
||||
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
|
||||
<meta
|
||||
name="viewport"
|
||||
content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"
|
||||
/>
|
||||
|
||||
<style>
|
||||
body {
|
||||
padding: 50px 80px;
|
||||
font: 14px "Helvetica Neue", Helvetica, sans-serif;
|
||||
font: 16px "Helvetica Neue", Helvetica, sans-serif;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 2em;
|
||||
font-size: 3em;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
p {
|
||||
color: #394351;
|
||||
}
|
||||
|
||||
pre {
|
||||
font-size: .8em;
|
||||
font-size: 0.8em;
|
||||
border-radius: 8px;
|
||||
padding: 12px 16px;
|
||||
background: #dae1e9;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="error">
|
||||
<h1>Error</h1>
|
||||
<p>Looks like something broke!</p>
|
||||
<pre>
|
||||
<code>
|
||||
//inject-stack//
|
||||
</code>
|
||||
</pre>
|
||||
<h1>//inject-status//</h1>
|
||||
<p>//inject-message//</p>
|
||||
<pre>//inject-stack//</pre>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -2,28 +2,36 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Error - //inject-status//</title>
|
||||
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
|
||||
<meta
|
||||
name="viewport"
|
||||
content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"
|
||||
/>
|
||||
|
||||
<style>
|
||||
body {
|
||||
padding: 50px 80px;
|
||||
font: 14px "Helvetica Neue", Helvetica, sans-serif;
|
||||
font: 16px "Helvetica Neue", Helvetica, sans-serif;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 2em;
|
||||
font-size: 3em;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
pre {
|
||||
font-size: .8em;
|
||||
p {
|
||||
color: #394351;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="error">
|
||||
<h1>Error</h1>
|
||||
<p>Looks like something broke!</p>
|
||||
<h1>//inject-status//</h1>
|
||||
<p>//inject-message//</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user