-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (38 loc) · 1.1 KB
/
index.js
File metadata and controls
39 lines (38 loc) · 1.1 KB
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
39
const defaultHTML = `<!DOCTYPE html>
<html>
<head><title>Oops</title></head>
<body>
<h3>Oops something wrong happened...</h3>
<b>That's our fault, your did nothing wrong.</b>
<p>Please bear with us, our team is already working on it.</p>
</body>
</html>`;
module.exports = ({ renderHTML, html } = {}) => async (ctx, next) => {
try {
await next();
} catch (err) {
const contentType = ctx.accepts('html', 'json');
const hasHtmlRenderer = typeof renderHTML === 'function' || html;
if (hasHtmlRenderer && contentType === 'html') {
ctx.type = 'text/html';
try {
ctx.body = html;
if (renderHTML) {
ctx.body = await renderHTML(ctx);
}
} catch (renderError) {
ctx.app.emit('error', renderError);
ctx.body = html || defaultHTML;
}
} else {
ctx.type = 'application/json';
ctx.body = {
statusCode: 500,
error: 'Internal Server Error',
message: 'An internal server error occurred'
};
}
ctx.status = 500;
ctx.app.emit('error', err);
}
};