import { ArgumentsHost, Catch, ExceptionFilter, ForbiddenException, UnauthorizedException } from '@nestjs/common';
import { error_message } from 'src/common/error_message';
import { LoggerService } from 'src/common/logger/logger.service';
const pjson = require('../../package.json');

@Catch(ForbiddenException)
export class ForbiddenInterceptor implements ExceptionFilter {
  constructor(
      private readonly logger: LoggerService
  ) {}
        
  catch(exception: ForbiddenException, host: ArgumentsHost) {
    const response = host.switchToHttp().getResponse();
    const message = error_message.unauthorized;
    const exceptionResponse = exception.getResponse() as any;

    this.logger.responseOut({
        error: true,
        statusCode: 403,
        message: error_message.forbidden
    })

    response.status(403).json({
      error: {
        code: 403,
        title: "forbidden",
        message: (exception.getResponse() as any).message,
      },
      data: {},
      meta: {
        version: pjson['version']
      }
    });
  }
}