All files / src/services sentry.client.ts

4.16% Statements 1/24
5.88% Branches 1/17
0% Functions 0/7
4.16% Lines 1/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                2x                                                                                                                                                                                                                        
// src/services/sentry.client.ts
/**
 * Sentry SDK initialization for client-side error tracking.
 * Implements ADR-015: Application Performance Monitoring and Error Tracking.
 *
 * This module configures @sentry/react to send errors to our self-hosted
 * Bugsink instance, which is Sentry-compatible.
 *
 * IMPORTANT: This module should be imported and initialized at the very top
 * of index.tsx, before any other imports, to ensure all errors are captured.
 */
import * as Sentry from '@sentry/react';
import config from '../config';
import { logger } from './logger.client';
 
/** Whether Sentry is properly configured (DSN present and enabled) */
export const isSentryConfigured = !!config.sentry.dsn && config.sentry.enabled;
 
/**
 * Initializes the Sentry SDK for the browser.
 * Should be called once at application startup.
 */
export function initSentry(): void {
  if (!isSentryConfigured) {
    logger.info('[Sentry] Error tracking disabled (VITE_SENTRY_DSN not configured)');
    return;
  }
 
  Sentry.init({
    dsn: config.sentry.dsn,
    environment: config.sentry.environment,
    debug: config.sentry.debug,
 
    // Performance monitoring - disabled for now to keep it simple
    tracesSampleRate: 0,
 
    // Capture console.error as breadcrumbs
    integrations: [
      Sentry.breadcrumbsIntegration({
        console: true,
        dom: true,
        fetch: true,
        history: true,
        xhr: true,
      }),
    ],
 
    // Filter out development-only errors and noise
    beforeSend(event) {
      // Skip errors from browser extensions
      if (
        event.exception?.values?.[0]?.stacktrace?.frames?.some((frame) =>
          frame.filename?.includes('extension://'),
        )
      ) {
        return null;
      }
      return event;
    },
  });
 
  logger.info(`[Sentry] Error tracking initialized (${config.sentry.environment})`);
}
 
/**
 * Captures an exception and sends it to Sentry.
 * Use this for errors that are caught and handled gracefully.
 */
export function captureException(
  error: Error,
  context?: Record<string, unknown>,
): string | undefined {
  if (!isSentryConfigured) {
    return undefined;
  }
 
  if (context) {
    Sentry.setContext('additional', context);
  }
 
  return Sentry.captureException(error);
}
 
/**
 * Captures a message and sends it to Sentry.
 * Use this for non-exception events that should be tracked.
 */
export function captureMessage(
  message: string,
  level: Sentry.SeverityLevel = 'info',
): string | undefined {
  if (!isSentryConfigured) {
    return undefined;
  }
 
  return Sentry.captureMessage(message, level);
}
 
/**
 * Sets the user context for all subsequent events.
 * Call this after user authentication.
 */
export function setUser(user: { id: string; email?: string; username?: string } | null): void {
  if (!isSentryConfigured) {
    return;
  }
 
  Sentry.setUser(user);
}
 
/**
 * Adds a breadcrumb to the current scope.
 * Breadcrumbs are logged actions that led up to an error.
 */
export function addBreadcrumb(breadcrumb: Sentry.Breadcrumb): void {
  if (!isSentryConfigured) {
    return;
  }
 
  Sentry.addBreadcrumb(breadcrumb);
}
 
// Re-export Sentry for advanced usage (Error Boundary, etc.)
export { Sentry };