mirror of
https://github.com/lingble/twenty.git
synced 2025-11-04 14:47:59 +00:00
* feat: wip exception handlers * feat: exception capturer * fix: rename exception-capturer into exception-handler * fix: remove unused variable
61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
import { DynamicModule, Global, Module } from '@nestjs/common';
|
|
|
|
import { ExceptionHandlerSentryDriver } from 'src/integrations/exception-handler/drivers/sentry.driver';
|
|
import { ExceptionHandlerConsoleDriver } from 'src/integrations/exception-handler/drivers/console.driver';
|
|
|
|
import { ExceptionHandlerService } from './exception-handler.service';
|
|
import { ExceptionHandlerDriver } from './interfaces';
|
|
import { EXCEPTION_HANDLER_DRIVER } from './exception-handler.constants';
|
|
import {
|
|
ConfigurableModuleClass,
|
|
OPTIONS_TYPE,
|
|
ASYNC_OPTIONS_TYPE,
|
|
} from './exception-handler.module-definition';
|
|
|
|
@Global()
|
|
@Module({
|
|
providers: [ExceptionHandlerService],
|
|
exports: [ExceptionHandlerService],
|
|
})
|
|
export class ExceptionHandlerModule extends ConfigurableModuleClass {
|
|
static forRoot(options: typeof OPTIONS_TYPE): DynamicModule {
|
|
const provider = {
|
|
provide: EXCEPTION_HANDLER_DRIVER,
|
|
useValue:
|
|
options.type === ExceptionHandlerDriver.Console
|
|
? new ExceptionHandlerConsoleDriver()
|
|
: new ExceptionHandlerSentryDriver(options.options),
|
|
};
|
|
const dynamicModule = super.forRoot(options);
|
|
|
|
return {
|
|
...dynamicModule,
|
|
providers: [...(dynamicModule.providers ?? []), provider],
|
|
};
|
|
}
|
|
|
|
static forRootAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule {
|
|
const provider = {
|
|
provide: EXCEPTION_HANDLER_DRIVER,
|
|
useFactory: async (...args: any[]) => {
|
|
const config = await options?.useFactory?.(...args);
|
|
|
|
if (!config) {
|
|
return null;
|
|
}
|
|
|
|
return config.type === ExceptionHandlerDriver.Console
|
|
? new ExceptionHandlerConsoleDriver()
|
|
: new ExceptionHandlerSentryDriver(config.options);
|
|
},
|
|
inject: options.inject || [],
|
|
};
|
|
const dynamicModule = super.forRootAsync(options);
|
|
|
|
return {
|
|
...dynamicModule,
|
|
providers: [...(dynamicModule.providers ?? []), provider],
|
|
};
|
|
}
|
|
}
|