2022-11-04 09:34:47 -07:00
|
|
|
import * as Logger from './LoggerProxy';
|
2023-11-27 06:33:21 -08:00
|
|
|
import { ApplicationError, type ReportingOptions } from './errors/application.error';
|
2022-11-04 09:34:47 -07:00
|
|
|
|
|
|
|
interface ErrorReporter {
|
|
|
|
report: (error: Error | string, options?: ReportingOptions) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const instance: ErrorReporter = {
|
2023-02-03 04:53:51 -08:00
|
|
|
report: (error) => {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
let e = error;
|
|
|
|
do {
|
2023-11-27 06:33:21 -08:00
|
|
|
const meta = e instanceof ApplicationError ? e.extra : undefined;
|
2023-11-24 05:42:46 -08:00
|
|
|
Logger.error(`${e.constructor.name}: ${e.message}`, meta);
|
2023-02-03 04:53:51 -08:00
|
|
|
e = e.cause as Error;
|
|
|
|
} while (e);
|
|
|
|
}
|
|
|
|
},
|
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;
|
2023-11-30 03:46:45 -08:00
|
|
|
if (typeof e === 'string') return new ApplicationError(e);
|
2022-11-04 09:34:47 -07:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const error = (e: unknown, options?: ReportingOptions) => {
|
|
|
|
const toReport = wrap(e);
|
|
|
|
if (toReport) instance.report(toReport, options);
|
|
|
|
};
|
|
|
|
|
2023-12-11 00:45:08 -08:00
|
|
|
export const report = error;
|
|
|
|
|
2022-11-04 09:34:47 -07:00
|
|
|
export const warn = (warning: Error | string, options?: ReportingOptions) =>
|
|
|
|
error(warning, { level: 'warning', ...options });
|