import { ArgumentsHost , Catch , ExceptionFilter } from '@nestjs/common' ; import { ApiErrorMessage , ApiErrorParams , ApiHttpResponseCode } from '@fc/app' ; import { ConfigService } from '@fc/config' ; import { Loggable , Trackable } from '@fc/exceptions' ; import { LoggerLevelNames , LoggerService } from '@fc/logger' ; import { TrackingService } from '@fc/tracking' ; import { TrackableEvent } from '../events' ; import { FcException } from '../exceptions' ; import { ExceptionsService } from '../exceptions.service' ; import { FcBaseExceptionFilter } from './fc-base.exception-filter' ; @ Catch ( FcException ) export class FcExceptionFilter extends FcBaseExceptionFilter implements ExceptionFilter { constructor ( protected readonly config , protected readonly logger , protected readonly tracking ) { super ( config , logger ) ; this . logger . setContext ( this . constructor . name ) ; } catch ( exception , host ) { this . logger . debug ( 'Exception from FcException' ) ; const res = host . switchToHttp ( ) . getResponse ( ) ; const req = host . switchToHttp ( ) . getRequest ( ) ; const code = ExceptionsService . getExceptionCodeFor ( exception ) ; const id = ExceptionsService . generateErrorId ( ) ; const { message } = exception ; /**
         * Business "exceptions" are by definition not technical issues
         * Thus they do not need to be logged as errors.
         *
         * They will most likely trigger a business log.
         */ const isTrackableError = Trackable . isTrackable ( exception ) ; const isLoggableError = Loggable . isLoggable ( exception ) ; if ( isLoggableError ) { this . logException ( code , id , exception ) ; } if ( isTrackableError ) { TODO see https this . tracking . track ( TrackableEvent , { req , exception } ) ; } /**
         * Do not render error if the `redirect` flag is set to true.
         * This usually means that the error is supposed to trigger a redirect
         * rather than to display some information.
         *
         * This is typically the case for many oidc parameters.
         * In those scenarios, redirection is handled by `oidc-provider`
         */ if ( exception . redirect === true ) { return ; } this . logger . trace ( { exception } , LoggerLevelNames . ERROR ) ; const httpErrorCode = ApiHttpResponseCode . ERROR_CODE_500 ; const errorMessage = { code , id , message } ; const exceptionParam = { res , error : errorMessage , httpResponseCode : httpErrorCode , } ; return this . errorOutput ( exceptionParam ) ; } see static ArgumentHostAdapter ( ctx ) { return { switchToHttp : ( ) => ( { getResponse : ( ) => ctx . res , getRequest : ( ) => ctx . req , } ) , } ; } }