All files / backend/koa-middleware catchError.js

100% Statements 5/5
100% Branches 0/0
100% Functions 2/2
100% Lines 5/5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20                  8x 3x 3x   2x         1x  
/**
 * Returns a koa middleware that will execute `next()` in a safe wrapper.
 * If `next()` throws during execution, we'll catch the error and emit it.
 *
 * @category middleware
 *
 * @return {Function} koa wrapper middleware
 */
function catchError() {
  return async (ctx, next) => {
    try {
      await next();
    } catch (error) {
      ctx.app.emit('error', error, ctx);
    }
  };
}
 
module.exports = catchError;