import { notificationContext } from './NotificationsViewer' ; const isPromise = < K , R > ( in_obj ) : in_obj is Promise < K > => { return Promise . resolve ( in_obj as Promise < K > ) === in_obj ; } ; /**
 * Extracts the error message and pushes to the notification list in the context
 */ export const CreateErrorObjAndSend = ( err ) => { const errObj = { message : ( typeof err === 'string' ? err : err . message ) } ; notificationContext . pushNotification ( errObj ) ; } ; /**
 * Print the error, push into notification context and throw again if needed
 */ const processError = ( err ) => { console . log ( err ) ; CreateErrorObjAndSend ( err ) ; } ; type anyFunction = ( ... args : any [ ] ) => any ; param anythingThatMightEmitError param catchErr return export function ErrorPopup < T extends anyFunction = anyFunction , K = ReturnType < T > extends Promise < infer R > ? R : ReturnType < T > > ( anythingThatMightEmitError , catchErr = true ) { try { const result = anythingThatMightEmitError ( ) ; if ( isPromise < K , ReturnType < T > > ( result ) ) { const caughtPromise = result . catch ( ( err ) => processError ( err ) ) ; if ( catchErr ) { return caughtPromise ; } return result ; } else { return Promise . resolve ( result ) ; } } catch ( err ) { processError ( err ) ; if ( catchErr ) { return Promise . resolve ( ) ; } else { return Promise . reject ( err ) ; } } }