aboutsummaryrefslogtreecommitdiff
path: root/lib/middleware/handle_errors.js
blob: f969676bb9b66d625de2b94e9d15b9856caec112 (plain)
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
/**
 * Middleware to create responses for unhandled errors.
 */
export default async function HandleErrors(next) {

  try {
    await next;
  }
  catch (err) {
    this.status = err.status || 500;

    const response = {
      error: err.message,
      status: this.status
    };

    if (response.status === 401) {
      response.error === 'Protected resource, use Authorization header to get access';
    }

    this.body = response;

    this.app.emit('error', err, this);
  }
}