diff --git a/package.json b/package.json index 0391198..75d8209 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bnc-notify", - "version": "1.6.1", + "version": "1.6.2", "description": "Show web3 users realtime transaction notifications", "keywords": [ "ethereum", diff --git a/src/interfaces.ts b/src/interfaces.ts index cd6292b..ad6f8eb 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,6 +1,7 @@ import type { BitcoinTransactionLog, - EthereumTransactionLog + EthereumTransactionLog, + SDKError } from 'bnc-sdk/dist/types/src/interfaces' export interface InitOptions extends ConfigOptions { @@ -8,8 +9,11 @@ export interface InitOptions extends ConfigOptions { transactionHandler?: TransactionHandler name?: string apiUrl?: string + onerror?: ErrorHandler } +export type ErrorHandler = (error: SDKError) => void + export interface TransactionHandler { (transaction: TransactionEvent): void } @@ -115,6 +119,7 @@ export interface AppStore { name?: string networkId?: number nodeSynced: boolean + onerror?: ErrorHandler mobilePosition: 'bottom' | 'top' desktopPosition: 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight' darkMode: boolean diff --git a/src/notify.ts b/src/notify.ts index 89cbe14..2cccd19 100644 --- a/src/notify.ts +++ b/src/notify.ts @@ -77,7 +77,7 @@ function init(options: InitOptions): API { validateInit(options) const { system, transactionHandler, apiUrl, ...appOptions } = options - const { dappId, networkId, name, clientLocale } = appOptions + const { dappId, networkId, name, clientLocale, onerror } = appOptions const transactionHandlers: TransactionHandler[] = [handleTransactionEvent] @@ -91,6 +91,7 @@ function init(options: InitOptions): API { blocknative = new BlocknativeSdk({ dappId, networkId, + onerror, transactionHandlers, name: name || 'Notify', apiUrl, @@ -98,10 +99,14 @@ function init(options: InitOptions): API { }) // filter out pending simulation events - blocknative.configuration({ - scope: 'global', - filters: [{ status: 'pending-simulation', _not: true }] - }) + blocknative + .configuration({ + scope: 'global', + filters: [{ status: 'pending-simulation', _not: true }] + }) + .catch(() => { + // swallow server timeout response error as we are not waiting on it + }) } // save config to app store @@ -151,12 +156,8 @@ function init(options: InitOptions): API { ) } - try { - const result = blocknative.account(address) - return result - } catch (error) { - throw new Error(error) - } + const result = blocknative.account(address) + return result } function hash(hash: string, id?: string) { @@ -166,12 +167,8 @@ function init(options: InitOptions): API { ) } - try { - const result = blocknative.transaction(hash, id) - return result - } catch (error) { - throw new Error(error) - } + const result = blocknative.transaction(hash, id) + return result } function transaction( @@ -188,7 +185,11 @@ function init(options: InitOptions): API { const emitter = createEmitter() const result = preflightTransaction(blocknative, options, emitter).catch( - err => err + err => { + const { onerror } = get(app) + onerror && onerror(err) + return err + } ) return { diff --git a/src/validation.ts b/src/validation.ts index e0e3df9..a2ea06f 100644 --- a/src/validation.ts +++ b/src/validation.ts @@ -11,6 +11,7 @@ const validInitKeys = [ 'system', 'transactionHandler', 'name', + 'onerror', 'mobilePosition', 'desktopPosition', 'darkMode', @@ -97,6 +98,7 @@ export function validateInit(init: InitOptions): void { transactionHandler, name, apiUrl, + onerror, ...otherParams } = init @@ -139,6 +141,13 @@ export function validateInit(init: InitOptions): void { optional: true }) + validateType({ + name: 'onerror', + value: onerror, + type: 'function', + optional: true + }) + validateConfig(otherParams) }