|
| 1 | +/** |
| 2 | + * Module dependencies |
| 3 | + */ |
| 4 | +import config from '../../config/index.js'; |
| 5 | +import sentryService from './sentry.js'; |
| 6 | +import analyticsService from './analytics.js'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Capture an exception, fanning out to all active trackers. |
| 10 | + * |
| 11 | + * - Sentry : active when `config.sentry.dsn` is set (and `enabled !== false`) |
| 12 | + * - PostHog : active when `config.posthog.apiKey` is set AND |
| 13 | + * `config.posthog.errorTracking === true` |
| 14 | + * |
| 15 | + * Safe no-op when neither tracker is configured. |
| 16 | + * |
| 17 | + * @param {Error} err - Error to capture |
| 18 | + * @param {Object} [ctx] - Optional context attached to the event |
| 19 | + * @param {string} [ctx.distinctId] - User identifier for PostHog |
| 20 | + * @param {string} [ctx.requestId] - Request trace ID |
| 21 | + * @returns {void} |
| 22 | + */ |
| 23 | +const captureException = (err, ctx = {}) => { |
| 24 | + // Sentry fan-out |
| 25 | + const sentryConfig = config?.sentry ?? {}; |
| 26 | + if (sentryConfig.dsn && sentryConfig.enabled !== false) { |
| 27 | + sentryService.captureException(err); |
| 28 | + } |
| 29 | + |
| 30 | + // PostHog fan-out — only when errorTracking is explicitly opted-in |
| 31 | + const posthogConfig = config?.posthog ?? {}; |
| 32 | + if (posthogConfig.apiKey && posthogConfig.errorTracking === true) { |
| 33 | + analyticsService.captureException(err, ctx); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * Capture an exception in PostHog only (skips Sentry). |
| 39 | + * Used inside the Express error middleware where Sentry's own Express handler |
| 40 | + * has already reported the error — calling captureException() here would |
| 41 | + * report it to Sentry a second time. |
| 42 | + * |
| 43 | + * @param {Error} err - Error to capture |
| 44 | + * @param {Object} [ctx] - Optional context |
| 45 | + * @returns {void} |
| 46 | + */ |
| 47 | +const captureExceptionPostHogOnly = (err, ctx = {}) => { |
| 48 | + const posthogConfig = config?.posthog ?? {}; |
| 49 | + if (posthogConfig.apiKey && posthogConfig.errorTracking === true) { |
| 50 | + analyticsService.captureException(err, ctx); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Initialise all configured trackers (Sentry + PostHog). |
| 56 | + * Safe to call when neither is configured. |
| 57 | + * @returns {Promise<void>} |
| 58 | + */ |
| 59 | +const init = async () => { |
| 60 | + await Promise.all([ |
| 61 | + sentryService.init(), |
| 62 | + analyticsService.init(), |
| 63 | + ]); |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * Set up Express error handling for all active trackers. |
| 68 | + * |
| 69 | + * Must be called after all routes are mounted. |
| 70 | + * Mounts Sentry's Express error handler first (captures structured request |
| 71 | + * context), then a PostHog-only fan-out middleware to avoid double-reporting |
| 72 | + * to Sentry (which is already covered by Sentry's own Express handler). |
| 73 | + * |
| 74 | + * @param {import('express').Express} app - Express application instance |
| 75 | + */ |
| 76 | +const setupExpressErrorHandler = (app) => { |
| 77 | + // Sentry Express handler (structured request/response context) |
| 78 | + sentryService.setupExpressErrorHandler(app); |
| 79 | + |
| 80 | + // PostHog-only fan-out middleware — Sentry already handled above |
| 81 | + // `_res` is required for Express to recognise this as a 4-arg error handler |
| 82 | + app.use((err, req, _res, next) => { |
| 83 | + const distinctId = req.user?._id |
| 84 | + ? String(req.user._id) |
| 85 | + : req.user?.id |
| 86 | + ? String(req.user.id) |
| 87 | + : 'anonymous'; |
| 88 | + captureExceptionPostHogOnly(err, { distinctId, requestId: req.id }); |
| 89 | + next(err); |
| 90 | + }); |
| 91 | +}; |
| 92 | + |
| 93 | +export default { |
| 94 | + init, |
| 95 | + captureException, |
| 96 | + captureExceptionPostHogOnly, |
| 97 | + setupExpressErrorHandler, |
| 98 | +}; |
0 commit comments