import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from "@nestjs/common";

/**
 * Basic filter for processing unhabdled HTTP exceptions
 */
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
    /**
     * Exception handling method
     * @param exception Execution object currently being processed
     * @param host Arguments host utility object
     */
    catch(exception: HttpException, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse();
        const status = exception.getStatus();

        response.status(status).json({
            status: status,
            message: exception.message,
        });
    }
}