0.1.1 • Published 7 years ago

prob v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

Prob

Error handling middleware for Koa 2

Requirements

Requires Node.js 7+ or Babel to support async/await

API

Prob(severity: String, message: String, [meta: Object, [status = 500: Integer]])

Example

const prob = require('prob');
const Prob = prob.Prob;

// Setup error handler for application
prob.setHandler(function(ctx, prob) {
  // Log to application log
  console.log('A problem occured in the app:', prob);

  // Handle response to client
  ctx.status = prob.status || 500;
  ctx.set('Content-Type', 'application/json');
  ctx.body = JSON.stringify({
    message: prob.message
  });
});

// Apply the middleware
router.use(prob.handle);

// Some route
router.post('/', async function(ctx, next) {
  try {
    dostuff();
  } catch (err) {
    throw new Prob('error', 'Could not do stuff', err, 500);
  }
});