11import log from './log' ;
22import type { TracingContext } from './tracing' ;
33
4- export type MessageNotifier = ( msg : string , tracingContext ?: TracingContext ) => void ;
5- export type ErrorNotifier = ( err : Error , tracingContext ?: TracingContext ) => void ;
4+ export type Notifier < T > = ( msg : T , tracingContext ?: TracingContext ) => void ;
5+ export type MessageNotifier = Notifier < string > ;
6+ export type ErrorNotifier = Notifier < Error > ;
67
78const messageNotifiers : MessageNotifier [ ] = [ ] ;
89const errorNotifiers : ErrorNotifier [ ] = [ ] ;
@@ -24,27 +25,35 @@ export function addErrorNotifier(notifier: ErrorNotifier) {
2425/**
2526 * Adds a callback to notify an error tracking service on both string error messages and JavaScript {@link Error}s.
2627 */
27- export function addNotifier ( notifier : ( msg : string | Error ) => void ) {
28+ export function addNotifier ( notifier : Notifier < string | Error > ) {
2829 messageNotifiers . push ( notifier ) ;
2930 errorNotifiers . push ( notifier ) ;
3031}
3132
33+ function notify < T > ( msg : T , tracingContext : TracingContext | undefined , notifiers : Notifier < T > [ ] ) {
34+ log . error ( `ErrorReporter notification: ${ msg } ` ) ;
35+ notifiers . forEach ( ( notifier ) => {
36+ try {
37+ notifier ( msg , tracingContext ) ;
38+ } catch ( e ) {
39+ log . error (
40+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
41+ `An error tracking notifier failed: ${ ( e as Error ) . message ?? e } \nStack:\n${ ( e as Error ) . stack } ` ,
42+ ) ;
43+ }
44+ } ) ;
45+ }
46+
3247/**
3348 * Reports an error message.
3449 */
3550export function message ( msg : string , tracingContext ?: TracingContext ) {
36- log . error ( `ErrorReporter notification: ${ msg } ` ) ;
37- messageNotifiers . forEach ( ( notifier ) => {
38- notifier ( msg , tracingContext ) ;
39- } ) ;
51+ notify ( msg , tracingContext , messageNotifiers ) ;
4052}
4153
4254/**
4355 * Reports an error.
4456 */
4557export function error ( err : Error , tracingContext ?: TracingContext ) {
46- log . error ( `ErrorReporter notification: ${ err } ` ) ;
47- errorNotifiers . forEach ( ( notifier ) => {
48- notifier ( err , tracingContext ) ;
49- } ) ;
58+ notify ( err , tracingContext , errorNotifiers ) ;
5059}
0 commit comments