Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.0 KB
Raw Blame
import * as Sentry from '@sentry/node'
import { FastifyBaseLogger } from 'fastify'

let sentryInitialized = false

export const exceptionHandler = {
    initializeSentry: (sentryDsn: string | undefined) => {
        if (!sentryDsn) {
            return
        }
        sentryInitialized = true
        Sentry.init({
            dsn: sentryDsn,
            beforeSend: (event) => {
                if (event?.exception?.values?.[0].type === 'AxiosError') {
                    return null
                }
                const value = event?.exception?.values?.[0]?.value
                if (value && ['EXECUTION_TIMEOUT', 'ENTITY_NOT_FOUND'].includes(value)) {
                    return null
                }
                return event
            },
        })
    },
    handle: (e: unknown, log: FastifyBaseLogger): void => {
        log.error({ error: e }, 'Unhandled exception')
        captureException(e)
    },
    captureException,
}

function captureException(e: unknown): void {
    if (sentryInitialized) {
        Sentry.captureException(e)
    }
}