aura-glass
Version:
A comprehensive glassmorphism design system for React applications with 142+ production-ready components
115 lines (112 loc) • 3.22 kB
JavaScript
import * as Sentry from '@sentry/react';
class ErrorHandler {
constructor() {
this.initialized = false;
this.initializeSentry();
}
initializeSentry() {
if (this.initialized || !process.env.SENTRY_DSN) return;
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV || 'development',
tracesSampleRate: process.env.NODE_ENV === 'production' ? 0.1 : 1.0,
integrations: [Sentry.replayIntegration({
maskAllText: true,
blockAllMedia: true
})],
beforeSend(event, hint) {
if (event.exception) {
const error = hint.originalException;
if (error && typeof error === 'object' && 'code' in error) {
if (error.code === 'insufficient_quota') {
console.warn('AI service quota exceeded, not sending to Sentry');
return null;
}
}
}
return event;
}
});
this.initialized = true;
}
handleError(error, context) {
const errorInfo = this.extractErrorInfo(error);
console.error(`[${context?.service || 'AI'}] Error in ${context?.operation || 'operation'}:`, {
message: errorInfo.message,
code: errorInfo.code,
details: errorInfo.details,
...context?.metadata
});
if (this.initialized && process.env.NODE_ENV === 'production') {
Sentry.captureException(error, {
tags: {
service: context?.service,
operation: context?.operation
},
extra: context?.metadata,
user: context?.userId ? {
id: context.userId
} : undefined
});
}
}
handleWithFallback(error, fallbackFn, context) {
this.handleError(error, context);
return fallbackFn();
}
async handleAsyncWithFallback(error, fallbackFn, context) {
this.handleError(error, context);
return fallbackFn();
}
wrapAsync(fn, context) {
return async (...args) => {
try {
return await fn(...args);
} catch (error) {
this.handleError(error, {
...context,
metadata: {
...context?.metadata,
functionName: fn.name,
arguments: args.slice(0, 3)
}
});
throw error;
}
};
}
extractErrorInfo(error) {
if (error instanceof Error) {
const info = {
message: error.message
};
if ('code' in error) info.code = error.code;
if ('response' in error) info.details = error.response;
if ('status' in error) info.status = error.status;
return info;
}
if (typeof error === 'string') {
return {
message: error
};
}
return {
message: 'Unknown error occurred',
details: error
};
}
createServiceError(message, code, statusCode = 500, details) {
return new ServiceError(message, code, statusCode, details);
}
}
class ServiceError extends Error {
constructor(message, code, statusCode = 500, details) {
super(message);
this.code = code;
this.statusCode = statusCode;
this.details = details;
this.name = 'ServiceError';
}
}
export { ErrorHandler, ServiceError };
//# sourceMappingURL=error-handler.js.map