From 4a667171df4b22ba9cd15576d022f3e88f3951ff Mon Sep 17 00:00:00 2001 From: Morley Zhi Date: Fri, 9 Aug 2019 12:25:04 -0400 Subject: [PATCH] Run stream errors through the onError function (#97) --- package.json | 2 +- src/data/DataProvider.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ec7cc0dd..fc867eb3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stellar/wallet-sdk", - "version": "0.0.4-rc.1", + "version": "0.0.4-rc.2", "description": "Libraries to help you write Stellar-enabled wallets in Javascript", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/data/DataProvider.ts b/src/data/DataProvider.ts index f3e8cbd4..5317d356 100644 --- a/src/data/DataProvider.ts +++ b/src/data/DataProvider.ts @@ -36,6 +36,10 @@ interface CallbacksObject { accountDetails?: () => void; } +interface ErrorHandlersObject { + accountDetails?: (error: any) => void; +} + export class DataProvider { private accountKey: string; private serverUrl: string; @@ -43,6 +47,7 @@ export class DataProvider { private effectStreamEnder?: () => void; private callbacks: CallbacksObject; + private errorHandlers: ErrorHandlersObject; constructor(params: DataProviderParams) { const accountKey = isAccount(params.accountOrKey) @@ -65,6 +70,7 @@ export class DataProvider { } this.callbacks = {}; + this.errorHandlers = {}; this.serverUrl = params.serverUrl; this.accountKey = accountKey; this.unfundedWatcherTimeout = null; @@ -195,6 +201,7 @@ export class DataProvider { .then(onMessage) .catch(onError); }, 2000); + this.errorHandlers.accountDetails = onError; this._startEffectWatcher().catch((err) => { onError(err); @@ -219,6 +226,7 @@ export class DataProvider { } delete this.callbacks.accountDetails; + delete this.errorHandlers.accountDetails; }; } @@ -318,6 +326,17 @@ export class DataProvider { } } }, + onerror: (e) => { + // run error handlers + const errorHandlers = Object.values(this.errorHandlers).filter( + (errorHandler) => !!errorHandler, + ); + if (errorHandlers.length) { + errorHandlers.forEach((errorHandler) => { + errorHandler(e); + }); + } + }, }); return Promise.resolve({});