UNPKG

@backtrace/nestjs

Version:
96 lines (95 loc) 3.77 kB
import { BacktraceClient } from '@backtrace/node'; import { ArgumentsHost } from '@nestjs/common/interfaces/index.js'; type ExceptionTypeFilter = (new (...args: never[]) => unknown)[] | ((err: unknown) => boolean); export interface BacktraceExceptionHandlerOptions<Context extends ArgumentsHost = ArgumentsHost> { /** * If specified, only matching errors will be sent. * * Can be an array of error types, or a function returning `boolean`. * The error is passed to the function as first parameter. * * @example * // Include only InternalServerErrorException or errors deriving from it * { * includeExceptionTypes: [InternalServerErrorException] * } * * // Include only errors that are instanceof InternalServerErrorException * { * includeExceptionTypes: (error) => error instanceof InternalServerErrorException * } */ readonly includeExceptionTypes?: ExceptionTypeFilter; /** * If specified, matching errors will not be sent. * Can be an array of error types, or a function returning `boolean`. * The error is passed to the function as first parameter. * * @example * // Exclude BadRequestException or errors deriving from it * { * excludeExceptionTypes: [BadRequestException] * } * * // Exclude errors that are instanceof BadRequestException * { * excludeExceptionTypes: (error) => error instanceof BadRequestException * } */ readonly excludeExceptionTypes?: ExceptionTypeFilter; /** * Will not throw on initialization if `true` and the `BacktraceClient` instance is `undefined`. * * If this is `true` and the client instance is not available, the interceptor will not be run. * * @default false */ readonly skipIfClientUndefined?: boolean; /** * This method will be called before sending the report. * Use this to build attributes that will be attached to the report. * * Note that this will overwrite the attributes. To add you own and keep the defaults, use `defaultAttributes`. * @param host Execution host. * @param defaultAttributes Attributes created by default by the interceptor. * @returns Attribute dictionary. * * @example * buildAttributes: (host, defaultAttributes) => ({ * ...defaultAttributes, * 'request.body': host.switchToHttp().getRequest().body * }) */ readonly buildAttributes?: (host: Context, defaultAttributes: Record<string, unknown>) => Record<string, unknown>; } export declare class BacktraceExceptionHandler<Context extends ArgumentsHost = ArgumentsHost> { private readonly _client?; private readonly _options; /** * Creates handler with the global client instance. * * The instance will be resolved while intercepting the request. * If the instance is not available, an error will be thrown. */ constructor(options?: BacktraceExceptionHandlerOptions<Context>); /** * Creates handler with the provided client instance. */ constructor(options: BacktraceExceptionHandlerOptions<Context> | undefined, client: BacktraceClient); handleException(err: unknown, host: Context): boolean; private shouldSend; private getBaseAttributes; private getTypeAttributes; private getHttpAttributes; private getRpcAttributes; private getWsAttributes; /** * Checks if given exception is matched by the filter, or any filter, if multiple are provided. * @param exception Exception to test. * @param filter Filter(s) to use. * @returns `true` if matched, `false` if not */ private testException; private static getDefaultOptions; } export {};