Files
twenty_crm/packages/twenty-server/src/utils/apply-cors-to-exceptions.ts
Charles Bochet 77c2961912 Fast follows on 0.34 (#9034)
Co-authored-by: Weiko <corentin@twenty.com>
2024-12-12 15:46:48 +00:00

40 lines
1.1 KiB
TypeScript

import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpException,
} from '@nestjs/common';
import { Response } from 'express';
// In case of exception in middleware run before the CORS middleware (eg: JSON Middleware that checks the request body),
// the CORS headers are missing in the response.
// This class add CORS headers to exception response to avoid misleading CORS error
@Catch()
export class UnhandledExceptionFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
if (!response.header) {
return;
}
// TODO: Check if needed, remove otherwise.
response.header('Access-Control-Allow-Origin', '*');
response.header(
'Access-Control-Allow-Methods',
'GET,HEAD,PUT,PATCH,POST,DELETE',
);
response.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept',
);
const status =
exception instanceof HttpException ? exception.getStatus() : 500;
response.status(status).json(exception.response);
}
}