import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus, Logger } from '@nestjs/common'
import { HttpAdapterHost } from '@nestjs/core'

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
    constructor(private readonly httpAdapterHost: HttpAdapterHost) {}

    catch(exception: unknown, host: ArgumentsHost): void {
        // 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 httpStatus = exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR

        const res = {
            status: httpStatus,
            path: httpAdapter.getRequestUrl(ctx.getRequest()),
            message: (exception as any).message
        }
        Logger.error(`status=${res.status}, path=${res.path}, msg=${res.message}`, 'GlobalExceptionFilter')

        httpAdapter.reply(ctx.getResponse(), res, httpStatus)
    }
}
