Skip to content
Snippets Groups Projects
exception.handler.ts 1.37 KiB
Newer Older
import type ResponseType from './response.js';
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';

import { Catch, HttpException, HttpStatus } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
zdravko iliev's avatar
zdravko iliev committed

@Catch()
export default class ExceptionHandler implements ExceptionFilter {
  public constructor(private readonly httpAdapterHost: HttpAdapterHost) {}
zdravko iliev's avatar
zdravko iliev committed

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  public catch(exception: any, host: ArgumentsHost): void {
zdravko iliev's avatar
zdravko iliev committed
    // In certain situations `httpAdapter` might not be available in the
    // constructor method, thus we should resolve it here.
    const { httpAdapter } = this.httpAdapterHost;

    const ctx = host.switchToHttp();
    const response = ctx.getResponse();

    let statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
    let message =
      exception.message.error || exception.message || 'Something went wrong!';

    if (exception instanceof HttpException) {
      const errorResponse: string | object = exception.getResponse();
zdravko iliev's avatar
zdravko iliev committed

      statusCode = exception.getStatus();
      message =
        (typeof errorResponse === 'object' &&
          Reflect.get(errorResponse, 'error')) ||
        message;
zdravko iliev's avatar
zdravko iliev committed
    }

    const responseBody: ResponseType = {
      statusCode,
      message,
      error: exception.message,
    };

    httpAdapter.reply(response, responseBody, statusCode);
  }
}