Fix view rest api in v2 (#15398)
# Introduction Initially wanted to reactive the test introduced in https://github.com/twentyhq/twenty/pull/15393, that was failing because of direct data source access removing all views ( even seeded one ) which was making the test fail While doing so discovered a lot of issue with the rest API: - Rest api wasn't consuming the v2 at all - Rest api wasn't prepared to handle v2 exceptions - Rest api did not handled unknown exceptions ( timeout ) Refactored the cleanup of each test to follow black box pattern and avoid test leakage
This commit is contained in:
+66
-22
@@ -2,47 +2,91 @@ import {
|
||||
type ArgumentsHost,
|
||||
Catch,
|
||||
type ExceptionFilter,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { type Response } from 'express';
|
||||
import { SOURCE_LOCALE } from 'twenty-shared/translations';
|
||||
|
||||
import { HttpExceptionHandlerService } from 'src/engine/core-modules/exception-handler/http-exception-handler.service';
|
||||
import { I18nService } from 'src/engine/core-modules/i18n/i18n.service';
|
||||
import {
|
||||
ViewSortException,
|
||||
ViewSortExceptionCode,
|
||||
} from 'src/engine/metadata-modules/view-sort/exceptions/view-sort.exception';
|
||||
import { type CustomException } from 'src/utils/custom-exception';
|
||||
import { WorkspaceMigrationBuilderExceptionV2 } from 'src/engine/workspace-manager/workspace-migration-v2/exceptions/workspace-migration-builder-exception-v2';
|
||||
import { fromWorkspaceMigrationBuilderExceptionToMetadataValidationResponseError } from 'src/engine/workspace-manager/workspace-migration-v2/interceptors/utils/from-workspace-migration-builder-exception-to-metadata-validation-response-error.util';
|
||||
import {
|
||||
type CustomException,
|
||||
UnknownException,
|
||||
} from 'src/utils/custom-exception';
|
||||
|
||||
@Catch(ViewSortException)
|
||||
@Injectable()
|
||||
@Catch(ViewSortException, WorkspaceMigrationBuilderExceptionV2)
|
||||
export class ViewSortRestApiExceptionFilter implements ExceptionFilter {
|
||||
constructor(
|
||||
private readonly httpExceptionHandlerService: HttpExceptionHandlerService,
|
||||
private readonly i18nService: I18nService,
|
||||
) {}
|
||||
|
||||
catch(exception: ViewSortException, host: ArgumentsHost) {
|
||||
catch(
|
||||
exception: ViewSortException | WorkspaceMigrationBuilderExceptionV2,
|
||||
host: ArgumentsHost,
|
||||
) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
|
||||
switch (exception.code) {
|
||||
case ViewSortExceptionCode.VIEW_SORT_NOT_FOUND:
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
404,
|
||||
);
|
||||
case ViewSortExceptionCode.INVALID_VIEW_SORT_DATA:
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
400,
|
||||
);
|
||||
default:
|
||||
// TODO: change to 500 when we have input validation
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
400,
|
||||
if (exception instanceof WorkspaceMigrationBuilderExceptionV2) {
|
||||
const i18n = this.i18nService.getI18nInstance(SOURCE_LOCALE);
|
||||
const { errors, summary } =
|
||||
fromWorkspaceMigrationBuilderExceptionToMetadataValidationResponseError(
|
||||
exception,
|
||||
i18n,
|
||||
);
|
||||
|
||||
return response.status(400).json({
|
||||
statusCode: 400,
|
||||
error: 'METADATA_VALIDATION_ERROR',
|
||||
message: exception.message || 'Validation failed',
|
||||
errors,
|
||||
summary,
|
||||
});
|
||||
}
|
||||
|
||||
if (exception instanceof ViewSortException) {
|
||||
switch (exception.code) {
|
||||
case ViewSortExceptionCode.VIEW_SORT_NOT_FOUND:
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
404,
|
||||
);
|
||||
case ViewSortExceptionCode.INVALID_VIEW_SORT_DATA:
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
400,
|
||||
);
|
||||
default:
|
||||
// TODO: change to 500 when we have input validation
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
400,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for any other exception type
|
||||
const unknownException = new UnknownException(
|
||||
'Internal server error',
|
||||
'INTERNAL_ERROR',
|
||||
);
|
||||
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
unknownException as CustomException,
|
||||
response,
|
||||
500,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user