All files / redis-smq-monitor/src/middlewares error-handler.ts

100% Statements 10/10
41.66% Branches 5/12
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 26 27 28 29 3057x 57x     57x 57x 57x   1x 1x 1x                                      
import { ValidationError } from 'class-validator';
import { registry } from '../lib/registry';
import { TMiddleware } from '../common/routing';
 
export const errorHandler: TMiddleware = async (ctx, next) => {
  try {
    await next();
  } catch (e: unknown) {
    registry.getItem('logger').error(JSON.stringify(e));
    ctx.status = e instanceof ValidationError ? 422 : 500;
    ctx.body = {
      error: {
        code: ctx.status,
        message:
          (e instanceof ValidationError && 'Validation error') ||
          (e instanceof Error && e.message) ||
          'Internal server error',
        details:
          (e instanceof ValidationError && e) ||
          (e instanceof Error && {
            message: e.message,
            name: e.name,
            stack: e.stack,
          }) ||
          {},
      },
    };
  }
};