2022-11-04 09:34:47 -07:00
|
|
|
import type { Primitives } from './utils';
|
|
|
|
import * as Logger from './LoggerProxy';
|
|
|
|
|
|
|
|
export interface ReportingOptions {
|
2023-01-11 09:28:35 -08:00
|
|
|
level?: 'warning' | 'error' | 'fatal';
|
2022-11-04 09:34:47 -07:00
|
|
|
tags?: Record<string, Primitives>;
|
|
|
|
extra?: Record<string, unknown>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ErrorReporter {
|
|
|
|
report: (error: Error | string, options?: ReportingOptions) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const instance: ErrorReporter = {
|
2022-11-22 05:00:36 -08:00
|
|
|
report: (error) =>
|
|
|
|
error instanceof Error && Logger.error(`${error.constructor.name}: ${error.message}`),
|
2022-11-04 09:34:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
export function init(errorReporter: ErrorReporter) {
|
|
|
|
instance.report = errorReporter.report;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrap = (e: unknown) => {
|
|
|
|
if (e instanceof Error) return e;
|
|
|
|
if (typeof e === 'string') return new Error(e);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const error = (e: unknown, options?: ReportingOptions) => {
|
|
|
|
const toReport = wrap(e);
|
|
|
|
if (toReport) instance.report(toReport, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const warn = (warning: Error | string, options?: ReportingOptions) =>
|
|
|
|
error(warning, { level: 'warning', ...options });
|