]>
Commit | Line | Data |
---|---|---|
1 | /** | |
2 | * Middleware to create responses for unhandled errors. | |
3 | */ | |
4 | export 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 | } |