54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import * as Sentry from '@sentry/nextjs';
|
|
import type {
|
|
ErrorReportingLevel,
|
|
ErrorReportingService,
|
|
ErrorReportingUser,
|
|
} from './error-reporting-service';
|
|
|
|
type SentryLike = typeof Sentry;
|
|
|
|
export type GlitchtipErrorReportingServiceOptions = {
|
|
enabled: boolean;
|
|
};
|
|
|
|
// GlitchTip speaks the Sentry protocol; @sentry/nextjs can send to GlitchTip via DSN.
|
|
export class GlitchtipErrorReportingService implements ErrorReportingService {
|
|
constructor(
|
|
private readonly options: GlitchtipErrorReportingServiceOptions,
|
|
private readonly sentry: SentryLike = Sentry
|
|
) {}
|
|
|
|
captureException(error: unknown, context?: Record<string, unknown>) {
|
|
if (!this.options.enabled) return undefined;
|
|
return this.sentry.captureException(error, context as any) as any;
|
|
}
|
|
|
|
captureMessage(message: string, level: ErrorReportingLevel = 'error') {
|
|
if (!this.options.enabled) return undefined;
|
|
return this.sentry.captureMessage(message, level as any) as any;
|
|
}
|
|
|
|
setUser(user: ErrorReportingUser | null) {
|
|
if (!this.options.enabled) return;
|
|
this.sentry.setUser(user as any);
|
|
}
|
|
|
|
setTag(key: string, value: string) {
|
|
if (!this.options.enabled) return;
|
|
this.sentry.setTag(key, value);
|
|
}
|
|
|
|
withScope<T>(fn: () => T, context?: Record<string, unknown>) {
|
|
if (!this.options.enabled) return fn();
|
|
|
|
return this.sentry.withScope((scope) => {
|
|
if (context) {
|
|
for (const [key, value] of Object.entries(context)) {
|
|
scope.setExtra(key, value);
|
|
}
|
|
}
|
|
return fn();
|
|
});
|
|
}
|
|
}
|