Skip to content

Latest commit

 

History

History
40 lines (29 loc) · 649 Bytes

File metadata and controls

40 lines (29 loc) · 649 Bytes

AppError

Install

npm i -S @bonjourjohn/app-error

Usage

With koa:

const koa = require('koa');
const AppError = require('@bonjourjohn/app-error');

const app = new koa();

app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    //if caught error is not AppError, convert it
    if (!(err instanceOf AppError)) {
      err = AppError.fromError(err);
    }

    // will only respond with JSON
    ctx.status = err.errno;
    ctx.body = {
      errno: err.errno,
      error: err.error
    };
  }
});

[...] //rest of your code

Testing

Run npm test after npm install.