Files
dbis_core/frontend/src/utils/errorTracking.ts

129 lines
3.0 KiB
TypeScript
Raw Permalink Normal View History

2026-01-02 20:27:42 -08:00
/**
* Error Tracking Utility
*
* Provides error tracking integration (ready for Sentry or similar services).
* Currently provides a no-op implementation that can be replaced with actual
* error tracking service integration.
*
* To integrate Sentry:
* 1. Install: npm install @sentry/react
* 2. Uncomment and configure the Sentry initialization
* 3. Update the captureException and captureMessage calls
*/
// Uncomment when ready to use Sentry:
// import * as Sentry from '@sentry/react';
interface ErrorContext {
[key: string]: unknown;
}
class ErrorTracker {
private initialized = false;
/**
* Initialize error tracking service
*/
init(dsn?: string, environment?: string): void {
if (this.initialized) {
return;
}
// Uncomment when ready to use Sentry:
/*
if (!dsn) {
console.warn('Error tracking DSN not provided, error tracking disabled');
return;
}
Sentry.init({
dsn,
environment: environment || import.meta.env.MODE,
integrations: [
new Sentry.BrowserTracing(),
new Sentry.Replay(),
],
tracesSampleRate: 1.0, // Adjust based on traffic
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
});
this.initialized = true;
*/
}
/**
* Capture an exception
*/
captureException(error: Error, context?: ErrorContext): void {
// Uncomment when ready to use Sentry:
/*
if (this.initialized) {
Sentry.captureException(error, {
contexts: {
custom: context || {},
},
});
}
*/
// Fallback logging
if (import.meta.env.DEV) {
console.error('Error captured:', error, context);
}
}
/**
* Capture a message
*/
captureMessage(message: string, level: 'info' | 'warning' | 'error' = 'error', context?: ErrorContext): void {
// Uncomment when ready to use Sentry:
/*
if (this.initialized) {
Sentry.captureMessage(message, {
level: level as Sentry.SeverityLevel,
contexts: {
custom: context || {},
},
});
}
*/
// Fallback logging
if (import.meta.env.DEV) {
const logMethod = level === 'error' ? console.error : level === 'warning' ? console.warn : console.info;
logMethod('Message captured:', message, context);
}
}
/**
* Set user context for error tracking
*/
setUser(user: { id: string; email?: string; username?: string } | null): void {
// Uncomment when ready to use Sentry:
/*
if (this.initialized) {
Sentry.setUser(user);
}
*/
}
/**
* Add breadcrumb for debugging
*/
addBreadcrumb(message: string, category?: string, level?: 'info' | 'warning' | 'error'): void {
// Uncomment when ready to use Sentry:
/*
if (this.initialized) {
Sentry.addBreadcrumb({
message,
category: category || 'custom',
level: level || 'info',
});
}
*/
}
}
export const errorTracker = new ErrorTracker();