diff options
author | Fabio Berger <me@fabioberger.com> | 2017-11-11 03:13:15 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-11-11 03:13:15 +0800 |
commit | 4262ac3c8966468b343c6467e0e9da85ef25be05 (patch) | |
tree | bf57b46afd2967b72fc3cd6d5d11d8efa82fb3af /src | |
parent | 76b66872d8f797ee3b0efe0fa2ca914cf44ee46c (diff) | |
download | dexon-0x-contracts-4262ac3c8966468b343c6467e0e9da85ef25be05.tar.gz dexon-0x-contracts-4262ac3c8966468b343c6467e0e9da85ef25be05.tar.zst dexon-0x-contracts-4262ac3c8966468b343c6467e0e9da85ef25be05.zip |
Fix unhandled promise rejection error on subscriptions
Diffstat (limited to 'src')
-rw-r--r-- | src/contract_wrappers/contract_wrapper.ts | 25 | ||||
-rw-r--r-- | src/types.ts | 4 |
2 files changed, 20 insertions, 9 deletions
diff --git a/src/contract_wrappers/contract_wrapper.ts b/src/contract_wrappers/contract_wrapper.ts index 19dccc6f2..a02465982 100644 --- a/src/contract_wrappers/contract_wrapper.ts +++ b/src/contract_wrappers/contract_wrapper.ts @@ -50,10 +50,14 @@ export class ContractWrapper { this._filterCallbacks[filterToken] = callback; return filterToken; } - protected _unsubscribe(filterToken: string): void { + protected _unsubscribe(filterToken: string, err?: Error): void { if (_.isUndefined(this._filters[filterToken])) { throw new Error(ZeroExError.SubscriptionNotFound); } + if (!_.isUndefined(err)) { + const callback = this._filterCallbacks[filterToken]; + callback(err, undefined); + } delete this._filters[filterToken]; delete this._filterCallbacks[filterToken]; if (_.isEmpty(this._filters)) { @@ -90,7 +94,7 @@ export class ContractWrapper { ...decodedLog, removed, }; - this._filterCallbacks[filterToken](logEvent); + this._filterCallbacks[filterToken](null, logEvent); } }); } @@ -122,11 +126,18 @@ export class ContractWrapper { delete this._blockAndLogStreamer; } private async _reconcileBlockAsync(): Promise<void> { - const latestBlock = await this._web3Wrapper.getBlockAsync(BlockParamLiteral.Latest); - // We need to coerce to Block type cause Web3.Block includes types for mempool blocks - if (!_.isUndefined(this._blockAndLogStreamer)) { - // If we clear the interval while fetching the block - this._blockAndLogStreamer will be undefined - this._blockAndLogStreamer.reconcileNewBlock(latestBlock as any as Block); + try { + const latestBlock = await this._web3Wrapper.getBlockAsync(BlockParamLiteral.Latest); + // We need to coerce to Block type cause Web3.Block includes types for mempool blocks + if (!_.isUndefined(this._blockAndLogStreamer)) { + // If we clear the interval while fetching the block - this._blockAndLogStreamer will be undefined + this._blockAndLogStreamer.reconcileNewBlock(latestBlock as any as Block); + } + } catch (err) { + const filterTokens = _.keys(this._filterCallbacks); + _.each(filterTokens, filterToken => { + this._unsubscribe(filterToken, err); + }); } } } diff --git a/src/types.ts b/src/types.ts index a9eac56d8..af3c36736 100644 --- a/src/types.ts +++ b/src/types.ts @@ -41,8 +41,8 @@ export type OrderValues = [BigNumber, BigNumber, BigNumber, export interface LogEvent<ArgsType> extends LogWithDecodedArgs<ArgsType> { removed: boolean; } -export type EventCallbackAsync<ArgsType> = (log: LogEvent<ArgsType>) => Promise<void>; -export type EventCallbackSync<ArgsType> = (log: LogEvent<ArgsType>) => void; +export type EventCallbackAsync<ArgsType> = (err: null|Error, log?: LogEvent<ArgsType>) => Promise<void>; +export type EventCallbackSync<ArgsType> = (err: null|Error, log?: LogEvent<ArgsType>) => void; export type EventCallback<ArgsType> = EventCallbackSync<ArgsType>|EventCallbackAsync<ArgsType>; export interface ExchangeContract extends Web3.ContractInstance { isValidSignature: { |