Fast follows on 0.34 (#9034)

Co-authored-by: Weiko <corentin@twenty.com>
This commit is contained in:
Charles Bochet
2024-12-12 16:46:48 +01:00
committed by GitHub
parent 05cd0d1803
commit 77c2961912
27 changed files with 141 additions and 76 deletions

View File

@ -9,12 +9,13 @@ import {
OPTIONS_TYPE,
} from 'src/engine/core-modules/exception-handler/exception-handler.module-definition';
import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service';
import { HttpExceptionHandlerService } from 'src/engine/core-modules/exception-handler/http-exception-handler.service';
import { ExceptionHandlerDriver } from 'src/engine/core-modules/exception-handler/interfaces';
@Global()
@Module({
providers: [ExceptionHandlerService],
exports: [ExceptionHandlerService],
providers: [ExceptionHandlerService, HttpExceptionHandlerService],
exports: [ExceptionHandlerService, HttpExceptionHandlerService],
})
export class ExceptionHandlerModule extends ConfigurableModuleClass {
static forRoot(options: typeof OPTIONS_TYPE): DynamicModule {

View File

@ -0,0 +1,51 @@
import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Response } from 'express';
import { ExceptionHandlerUser } from 'src/engine/core-modules/exception-handler/interfaces/exception-handler-user.interface';
import { ExceptionHandlerWorkspace } from 'src/engine/core-modules/exception-handler/interfaces/exception-handler-workspace.interface';
import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service';
import { CustomException } from 'src/utils/custom-exception';
export const handleException = (
exception: CustomException,
exceptionHandlerService: ExceptionHandlerService,
user?: ExceptionHandlerUser,
workspace?: ExceptionHandlerWorkspace,
): void => {
exceptionHandlerService.captureExceptions([exception], { user, workspace });
};
interface RequestAndParams {
request: Request | null;
params: any;
}
@Injectable({ scope: Scope.REQUEST })
export class HttpExceptionHandlerService {
constructor(
private readonly exceptionHandlerService: ExceptionHandlerService,
@Inject(REQUEST)
private readonly request: RequestAndParams | null,
) {}
handleError = (
exception: CustomException,
response: Response<any, Record<string, any>>,
errorCode?: number,
user?: ExceptionHandlerUser,
workspace?: ExceptionHandlerWorkspace,
): Response<any, Record<string, any>> | undefined => {
const params = this.request?.params;
if (params?.workspaceId)
workspace = { ...workspace, id: params.workspaceId };
if (params?.userId) user = { ...user, id: params.userId };
handleException(exception, this.exceptionHandlerService, user, workspace);
return response.status(errorCode || 500).send(exception.message);
};
}