]> git.r.bdr.sh - rbdr/forum/blame - lib/middleware/handle_errors.js
Add a README
[rbdr/forum] / lib / middleware / handle_errors.js
CommitLineData
2d5b700e
BB
1/**
2 * Middleware to create responses for unhandled errors.
3 */
4export default async function HandleErrors(next) {
5
6 try {
7 await next;
8 }
9 catch (err) {
10 this.status = err.status || 500;
11
12 const response = {
13 error: err.message,
14 status: this.status
15 };
16
17 if (response.status === 401) {
18 response.error === 'Protected resource, use Authorization header to get access';
19 }
20
21 this.body = response;
22
23 this.app.emit('error', err, this);
24 }
25}