From 49136de47be3dc662f74331ee79c06f3d593c9da Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 8 Nov 2018 13:56:51 +0100 Subject: [PATCH] Catch errors while subscribing (#385) --- packages/rpc-core/src/index.ts | 2 +- packages/rpc-rx/src/index.ts | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/rpc-core/src/index.ts b/packages/rpc-core/src/index.ts index 064d8552b3..5d13350a31 100644 --- a/packages/rpc-core/src/index.ts +++ b/packages/rpc-core/src/index.ts @@ -81,7 +81,7 @@ export default class Rpc implements RpcInterface { return `${method} (${inputs}): ${type}`; } - private createInterface ({ methods, section }: RpcSection): RpcInterface$Section { + private createInterface ({ methods }: RpcSection): RpcInterface$Section { return Object .keys(methods) .reduce((exposed, method) => { diff --git a/packages/rpc-rx/src/index.ts b/packages/rpc-rx/src/index.ts index c7048b599d..a223bf6b08 100644 --- a/packages/rpc-rx/src/index.ts +++ b/packages/rpc-rx/src/index.ts @@ -8,7 +8,7 @@ import { RpcRxInterface, RpcRxInterface$Events, RpcRxInterface$Section } from '. import EventEmitter from 'eventemitter3'; import { BehaviorSubject, Observable, Subscriber, from } from 'rxjs'; -import { publishReplay, refCount } from 'rxjs/operators'; +import { map, publishReplay, refCount } from 'rxjs/operators'; import Rpc from '@polkadot/rpc-core/index'; import { isFunction, isUndefined } from '@polkadot/util'; @@ -134,20 +134,22 @@ export default class RpcRx implements RpcRxInterface { private createReplay (name: string, params: Array, section: RpcInterface$Section, subName: string, paramStr: string): Observable { return Observable .create((observer: Subscriber): Function => { - try { - const fn = section[name]; - const subscribe = fn(...params, this.createReplayCallback(observer)); + const fn = section[name]; + const callback = this.createReplayCallback(observer); + const subscribe = fn(...params, callback).catch((error) => + observer.next(error) + ); - return this.createReplayUnsub(fn, subscribe, subName, paramStr); - } catch (error) { - console.error(error); - - return (): void => { - console.error('Unsubscribe called on previously failed subscription', error); - }; - } + return this.createReplayUnsub(fn, subscribe, subName, paramStr); }) .pipe( + map((value) => { + if (value instanceof Error) { + throw value; + } + + return value; + }), publishReplay(1), refCount() );