|
| 1 | +import * as Sentry from '@sentry/node'; |
| 2 | +import { createRequire } from 'module'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Initialize Sentry for error tracking |
| 6 | + * @param dsn Optional custom DSN to use instead of the default |
| 7 | + */ |
| 8 | +export function initSentry(dsn?: string) { |
| 9 | + // Get package version using createRequire for ES modules |
| 10 | + let packageVersion = 'unknown'; |
| 11 | + try { |
| 12 | + const require = createRequire(import.meta.url); |
| 13 | + packageVersion = process.env.npm_package_version || require('../../package.json').version; |
| 14 | + } catch (error) { |
| 15 | + console.warn('Could not determine package version for Sentry:', error); |
| 16 | + } |
| 17 | + |
| 18 | + // Initialize Sentry |
| 19 | + Sentry.init({ |
| 20 | + // Default DSN from Sentry.io integration instructions |
| 21 | + dsn: dsn || 'https://2873d2518b60f645918b6a08ae5e69ae@o4508898407481344.ingest.us.sentry.io/4508898476687360', |
| 22 | + |
| 23 | + // No profiling integration as requested |
| 24 | + |
| 25 | + // Capture 100% of the transactions |
| 26 | + tracesSampleRate: 1.0, |
| 27 | + |
| 28 | + // Set environment based on NODE_ENV |
| 29 | + environment: process.env.NODE_ENV || 'development', |
| 30 | + |
| 31 | + // Add release version from package.json |
| 32 | + release: `mycoder@${packageVersion}`, |
| 33 | + |
| 34 | + // Don't capture errors in development mode unless explicitly enabled |
| 35 | + enabled: process.env.NODE_ENV !== 'development' || process.env.ENABLE_SENTRY === 'true', |
| 36 | + }); |
| 37 | + |
| 38 | + // Log confirmation that Sentry is initialized with version info |
| 39 | + if (process.env.NODE_ENV !== 'test') { |
| 40 | + console.log(`Sentry initialized for mycoder@${packageVersion}`); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Capture an exception with Sentry |
| 46 | + * @param error Error to capture |
| 47 | + */ |
| 48 | +export function captureException(error: Error | unknown) { |
| 49 | + Sentry.captureException(error); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Capture a message with Sentry |
| 54 | + * @param message Message to capture |
| 55 | + * @param level Optional severity level |
| 56 | + */ |
| 57 | +export function captureMessage(message: string, level?: Sentry.SeverityLevel) { |
| 58 | + Sentry.captureMessage(message, level); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Test Sentry error reporting by throwing a test error |
| 63 | + */ |
| 64 | +export function testSentryErrorReporting() { |
| 65 | + try { |
| 66 | + // Get package version for the error message |
| 67 | + let packageVersion = 'unknown'; |
| 68 | + try { |
| 69 | + const require = createRequire(import.meta.url); |
| 70 | + packageVersion = process.env.npm_package_version || require('../../package.json').version; |
| 71 | + } catch (error) { |
| 72 | + console.warn('Could not determine package version for test error:', error); |
| 73 | + } |
| 74 | + |
| 75 | + // Throw a test error with version information |
| 76 | + throw new Error(`Test error for Sentry.io integration from mycoder@${packageVersion}`); |
| 77 | + } catch (error) { |
| 78 | + // Capture the error with Sentry |
| 79 | + Sentry.captureException(error); |
| 80 | + |
| 81 | + // Log a message about the test |
| 82 | + console.log('Test error sent to Sentry.io'); |
| 83 | + |
| 84 | + // Return the error for inspection |
| 85 | + return error; |
| 86 | + } |
| 87 | +} |
0 commit comments