import { ErrorHandler , Injectable } from '@angular/core' ; export enum ErrorCategory { CORDOVA_PLUGIN = 'cordova_plugin' , IONIC_MODAL = 'ionic_modal' , IONIC_ALERT = 'ionic_alert' , IONIC_LOADER = 'ionic_loader' , IONIC_NAVIGATION = 'navigation' , WALLET_SERVICE = 'wallet_service' , SCHEME_ROUTING = 'scheme_routing' , SECURE_STORAGE = 'secure_storage' , INIT_CHECK = 'init_check' , ENTROPY_COLLECTION = 'entropy_collection' , INTERACTION_SERVICE = 'interaction_service' , DEEPLINK_SERVICE = 'deeplink_service' , OTHER = 'other' } const NUMBER_OF_ERRORS_CACHED = 100 ; export type ErrorHistoryObject = [ ErrorCategory , string , string , string , number ] ; export class LocalErrorLogger { private readonly errorHistoryKey : string = 'airgap-vault:ERROR_HISTORY' ; constructor ( ) { } public async addLog ( category , error ) { const storedErrors = ( await this . getErrorHistory ( ) ) . slice ( 0 , NUMBER_OF_ERRORS_CACHED - 1 ) ; storedErrors . unshift ( [ category , error . name , error . message , error . stack , new Date ( ) . getTime ( ) ] ) ; localStorage . setItem ( this . errorHistoryKey , JSON . stringify ( storedErrors ) ) ; } public async getErrorHistory ( ) { try { return JSON . parse ( localStorage . getItem ( this . errorHistoryKey ) ) || [ ] ; } catch ( e ) { return [ ] ; } } public async getErrorHistoryFormatted ( ) { const errorHistory = await this . getErrorHistory ( ) ; return errorHistory . map ( ( [ category , name , message , stack , date ] ) => { return `[${ category }](${ date })\n${ name }:\n${ message }\n${ stack }` ; } ) . join ( '\n\n' ) ; } public async clearAll ( ) { localStorage . setItem ( this . errorHistoryKey , JSON . stringify ( [ ] ) ) ; } } const errorLogger = new LocalErrorLogger ( ) ; type ErrorCallback = ( error : Error & { originalError ? : Error ; } ) => void ; const handleErrorLocal = ( category ? ) : ErrorCallback => { return ( error ) : void => { console . log ( 'saving error locally, category' , category , error ) ; errorLogger . addLog ( category , error ) ; } ; } ; const handleErrorIgnore = ( error ) : void => { console . log ( 'ignoring error' ) ; console . error ( error . originalError || error ) ; } ; export { handleErrorIgnore , handleErrorLocal } ; @ Injectable ( ) export class ErrorHandlerService extends ErrorHandler { public handleError ( error ) { super . handleError ( error ) ; handleErrorLocal ( ErrorCategory . OTHER ) ( error ) ; } }