62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import { sanitizeErrorReportValue } from "./sanitize.js";
|
|
|
|
export type ErrorReportSeverity = "debug" | "info" | "warn" | "error" | "fatal";
|
|
|
|
export interface ErrorReportContext {
|
|
component: string;
|
|
operation: string;
|
|
severity?: ErrorReportSeverity;
|
|
requestId?: string;
|
|
method?: string;
|
|
path?: string;
|
|
queueName?: string;
|
|
jobId?: string | number;
|
|
retryCount?: number;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
export interface ErrorReportEvent extends ErrorReportContext {
|
|
error?: unknown;
|
|
errorName?: string;
|
|
errorMessage?: string;
|
|
errorStack?: string;
|
|
}
|
|
|
|
export interface ErrorReporter {
|
|
report(event: ErrorReportEvent): void | Promise<void>;
|
|
}
|
|
|
|
export function createNoopErrorReporter(): ErrorReporter {
|
|
return {
|
|
report() {
|
|
// Intentionally empty: the no-op reporter is safe for tests and local defaults.
|
|
},
|
|
};
|
|
}
|
|
|
|
export function reportError(reporter: ErrorReporter | undefined | null, event: ErrorReportEvent): void {
|
|
if (!reporter) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const result = reporter.report(sanitizeErrorReportValue(event) as ErrorReportEvent);
|
|
|
|
if (isPromiseLike(result)) {
|
|
result.catch(() => {
|
|
// Reporter failures must never mask the runtime failure being reported.
|
|
});
|
|
}
|
|
} catch {
|
|
// Reporter failures must never mask the runtime failure being reported.
|
|
}
|
|
}
|
|
|
|
function isPromiseLike(value: unknown): value is PromiseLike<void> {
|
|
return Boolean(value) && typeof (value as { catch?: unknown }).catch === "function";
|
|
}
|
|
|
|
export { sanitizeErrorReportValue } from "./sanitize.js";
|
|
export { createSentryErrorReporter, type SentryErrorReporterOptions, type SentryLikeModule, type SentryInitOptions } from "./sentry.js";
|
|
export { initializeTracing, shutdownTracing, type TracingOptions, type NodeSdkLikeModule, type NodeSdkInstance, type OtlpExporterLikeModule } from "./tracing.js";
|