Home Manual Reference Source Test Repository

src/error_action.js

import {app} from './application';

/**
 * Manages the application errors.
 * @param {Context} ctx The context encapsulating the client request and the server response.
 * @param {function} next TODO
 */
export async function errorAction(ctx, next) {
  try {
    await next();
  }

  catch (err) {
    let {message, name, status} = err;
    let model = {message, name, status: typeof status == 'number' ? status : 500};
    if (app().debug) model.stack = err.stack;

    ctx.body = model;
    ctx.status = model.status;
  }
}