mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 05:47:52 +00:00
110 lines
3.0 KiB
TypeScript
110 lines
3.0 KiB
TypeScript
import * as Sentry from '@sentry/react';
|
|
import { analytics } from '../lib/analytics';
|
|
|
|
interface ErrorTrackingOptions {
|
|
category?: string;
|
|
userId?: string;
|
|
userEmail?: string;
|
|
}
|
|
|
|
export function useErrorTracking(options: ErrorTrackingOptions = {}) {
|
|
const { category = 'error', userId, userEmail } = options;
|
|
|
|
const trackError = (
|
|
error: Error,
|
|
context?: {
|
|
componentName?: string;
|
|
action?: string;
|
|
metadata?: Record<string, any>;
|
|
}
|
|
) => {
|
|
// 1. Rastreia no Sentry primeiro (para debug técnico)
|
|
Sentry.withScope((scope) => {
|
|
// Adiciona contexto do usuário
|
|
if (userId) scope.setUser({ id: userId, email: userEmail });
|
|
|
|
// Adiciona contexto do erro
|
|
if (context?.componentName) scope.setTag('component', context.componentName);
|
|
if (context?.action) scope.setTag('action', context.action);
|
|
if (context?.metadata) scope.setContext('metadata', context.metadata);
|
|
|
|
// Captura o erro
|
|
Sentry.captureException(error);
|
|
});
|
|
|
|
// 2. Rastreia no Rudderstack (para analytics)
|
|
analytics.track('error_occurred', {
|
|
category,
|
|
error_name: error.name,
|
|
error_message: error.message,
|
|
error_stack: error.stack,
|
|
component: context?.componentName,
|
|
action: context?.action,
|
|
...context?.metadata,
|
|
// Informações do ambiente
|
|
url: window.location.href,
|
|
user_agent: navigator.userAgent,
|
|
});
|
|
};
|
|
|
|
const trackErrorBoundary = (
|
|
error: Error,
|
|
componentStack: string,
|
|
componentName: string
|
|
) => {
|
|
// 1. Rastreia no Sentry
|
|
Sentry.withScope((scope) => {
|
|
if (userId) scope.setUser({ id: userId, email: userEmail });
|
|
scope.setTag('component', componentName);
|
|
scope.setTag('error_type', 'react_error_boundary');
|
|
scope.setExtra('componentStack', componentStack);
|
|
Sentry.captureException(error);
|
|
});
|
|
|
|
// 2. Rastreia no Rudderstack
|
|
analytics.track('error_boundary_triggered', {
|
|
category,
|
|
error_name: error.name,
|
|
error_message: error.message,
|
|
component_name: componentName,
|
|
component_stack: componentStack,
|
|
url: window.location.href,
|
|
user_agent: navigator.userAgent,
|
|
});
|
|
};
|
|
|
|
const trackApiError = (
|
|
error: any,
|
|
endpoint: string,
|
|
method: string,
|
|
requestData?: any
|
|
) => {
|
|
// 1. Rastreia no Sentry
|
|
Sentry.withScope((scope) => {
|
|
if (userId) scope.setUser({ id: userId, email: userEmail });
|
|
scope.setTag('error_type', 'api_error');
|
|
scope.setTag('endpoint', endpoint);
|
|
scope.setTag('method', method);
|
|
scope.setContext('request', { data: requestData });
|
|
Sentry.captureException(error);
|
|
});
|
|
|
|
// 2. Rastreia no Rudderstack
|
|
analytics.track('api_error_occurred', {
|
|
category,
|
|
endpoint,
|
|
method,
|
|
error_name: error.name,
|
|
error_message: error.message,
|
|
status_code: error.status || error.statusCode,
|
|
request_data: requestData,
|
|
url: window.location.href,
|
|
});
|
|
};
|
|
|
|
return {
|
|
trackError,
|
|
trackErrorBoundary,
|
|
trackApiError,
|
|
};
|
|
}
|