import { Inject , Injectable , Injector } from '@angular/core' ; import { TuiNotification } from '@taiga-ui/core' ; import { PolymorpheusComponent } from '@tinkoff/ng-polymorpheus' ; import { TranslateService } from '@ngx-translate/core' ; import { RubicError } from '@core/errors/models/rubic-error' ; import { NotificationsService } from 'src/app/core/services/notifications/notifications.service' ; import { UnknownErrorComponent } from 'src/app/core/errors/components/unknown-error/unknown-error.component' ; import { UnknownError } from 'src/app/core/errors/models/unknown.error' ; import { CUSTOM_RPC_ERROR } from '@core/errors/models/standard/custom-rpc-error' ; import { EIP_1474 } from '@core/errors/models/standard/eip-1474' ; import { EIP_1193 } from '@core/errors/models/standard/eip-1193' ; import { ERROR_TYPE } from '@core/errors/models/error-type' ; import { RubicWarning } from '@core/errors/models/rubic-warning' ; interface Question { title : string ; answer : string ; } @ Injectable ( { providedIn : 'root' } ) export class ErrorsService { public questions : Question [ ] ; constructor ( private readonly notificationsService , @ Inject ( Injector ) private injector , private translateService ) { } deprecated param error public throw ( error ) { this . catch ( error ) ; throw error ; } param error public catchAnyError ( error ) { if ( error instanceof RubicError ) { this . catch ( error ) ; } else { console . debug ( error ) ; this . catch ( new UnknownError ( ) ) ; } } param error public catch ( error ) { console . debug ( error ) ; if ( error . displayError === false || error . message ?. includes ( 'Attempt to use a destroyed view' ) ) { return ; } const isWarning = error instanceof RubicWarning ; const options = { label : this . translateService . instant ( isWarning ? 'common.warning' : 'common.error' ) , status : isWarning ? TuiNotification . Warning : TuiNotification . Error , data : { } , autoClose : 7000 } ; if ( this . isCustomRPCError ( error ) ) { const errorComponent = new PolymorpheusComponent ( error . component || UnknownErrorComponent , this . injector ) ; this . notificationsService . show ( errorComponent , options ) ; } if ( error ?. type === ERROR_TYPE . COMPONENT || error ?. type === ERROR_TYPE . RAW_MESSAGE || this . isRPCError ( error ) ) { const errorComponent = new PolymorpheusComponent ( error . component || UnknownErrorComponent , this . injector ) ; options . data = error ?. data || error ; this . notificationsService . show ( errorComponent , options ) ; return ; } const text = error ?. translateKey ? this . translateService . instant ( error . translateKey , error ?. data ) : error . message ; this . notificationsService . show ( text , options ) ; } param rpcError param currentError return public findRPCError ( rpcError , currentError ) { return ( currentError . message . includes ( rpcError . code ) || currentError . message . toLocaleLowerCase ( ) . includes ( rpcError . message . toLocaleLowerCase ( ) ) || ( currentError ?. code && String ( currentError . code ) === rpcError . code ) ) ; } param currentError return private isRPCError ( currentError ) { return ( EIP_1193 . some ( rpcError => this . findRPCError ( rpcError , currentError ) ) || EIP_1474 . some ( rpcError => this . findRPCError ( rpcError , currentError ) ) ) ; } param currentError return private isCustomRPCError ( currentError ) { return CUSTOM_RPC_ERROR . some ( rpcError => this . findRPCError ( rpcError , currentError ) ) ; } }