import { ArgumentsHost, Catch, ExceptionFilter, HttpStatus } from '@nestjs/common';
import type { Request, Response } from 'express';
import { UnknownCorpSlugError } from './corp-filter.util';

@Catch(UnknownCorpSlugError)
export class UnknownCorpSlugFilter implements ExceptionFilter<UnknownCorpSlugError> {
  catch(exception: UnknownCorpSlugError, host: ArgumentsHost): void {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();

    response.status(HttpStatus.BAD_REQUEST).json({
      statusCode: HttpStatus.BAD_REQUEST,
      message: exception.message,
      error: 'Bad Request',
      path: request.url,
    });
  }
}
