import electronIsDev from 'electron-is-dev' ; import ErrorStackParser from 'error-stack-parser' ; import { errorReport } from 'root/api/erroreporter' ; import { log } from 'root/app/log-rotate/log_rotate' ; import { settingsStore } from 'root/app/settings-store/settings_store' ; import { asMap , asString } from 'root/lib/type_utils' ; interface LogProperties { [ key ] : any ; } interface ErrorWithStack { stack : string ; } function getStacktrace ( err ? ) { const stack = asString ( asMap ( err , { } ) . stack ) ; if ( stack !== undefined ) { return err as ErrorWithStack ; } const errorWithStack = { } ; Error . captureStackTrace ( errorWithStack ) ; return errorWithStack as ErrorWithStack ; } export function error ( msg , err , extra = { } , onlyLocal ? ) { const errorWithStack = getStacktrace ( err ) ; const TheTrace = ErrorStackParser . parse ( errorWithStack as Error ) ; TheTrace . shift ( ) ; TheTrace . shift ( ) ; const TheError = TheTrace . shift ( ) ; if ( electronIsDev ) { console . error ( { msg , err : String ( err ) , type : 'Error' , ... extra , stack : errorWithStack . stack } ) ; } else { const account = settingsStore . getAccount ( ) ; if ( onlyLocal ) { log ( `${ JSON . stringify ( { date : new Date ( ) . toISOString ( ) , msg , err , stacktrace : errorWithStack . stack , extra } ) }` ) ; } else { errorReport ( account ?. token , TheError ?. fileName , TheError ?. lineNumber , TheError ?. columnNumber , TheError ?. functionName , msg , JSON . stringify ( { err , stacktrace : errorWithStack . stack , extra } ) ) . then ( ( res ) => { if ( ! res ) { log ( `${ JSON . stringify ( { date : new Date ( ) . toISOString ( ) , msg , err , stacktrace : errorWithStack . stack , extra } ) }` ) ; } } ) . catch ( ( _ ) => { log ( `${ JSON . stringify ( { date : new Date ( ) . toISOString ( ) , msg , err , stacktrace : errorWithStack . stack , extra } ) }` ) ; } ) ; } } }