Skip to content

Commit

Permalink
Fix getStatus is not a function
Browse files Browse the repository at this point in the history
  • Loading branch information
martmull committed May 21, 2024
1 parent 0654e89 commit 20538a1
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions packages/twenty-server/src/utils/apply-cors-to-exceptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ExceptionFilter, Catch, ArgumentsHost, Logger } from '@nestjs/common';
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
} from '@nestjs/common';

import { Response } from 'express';

Expand All @@ -7,27 +12,27 @@ import { Response } from 'express';
// This class add CORS headers to exception response to avoid misleading CORS error
@Catch()
export class ApplyCorsToExceptions implements ExceptionFilter {
private readonly logger = new Logger(ApplyCorsToExceptions.name);
catch(exception: any, host: ArgumentsHost) {
try {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();

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',
);

response.status(exception.getStatus()).json(exception.response);
} catch (e) {
this.logger.error(e);
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();

if (!response.header) {
return;
}

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);
}
}

0 comments on commit 20538a1

Please sign in to comment.