From b3d7278416b5125a0d15f5ee17b2bb0d855bcae4 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Thu, 17 Aug 2017 18:21:44 +0300 Subject: [PATCH] fix(subscribe): Throw fatal if error handler is not present (#477) * Throw error if handler is not present * Bind context to error handler * Prefer passing context instead of bind --- src/observable/subscribe.js | 32 ++++++++++++++++++++----------- test/observable/subscribe-test.js | 13 +++++++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/observable/subscribe.js b/src/observable/subscribe.js index 3c9a279e..66ccf264 100644 --- a/src/observable/subscribe.js +++ b/src/observable/subscribe.js @@ -34,13 +34,23 @@ SubscribeObserver.prototype.event = function (t, x) { SubscribeObserver.prototype.end = function (t, x) { if (!this.disposable.disposed) { var s = this.subscriber - doDispose(this.fatalError, s, s.complete, s.error, this.disposable, x) + var fatalError = this.fatalError + Promise.resolve(this.disposable.dispose()).then(function () { + if (typeof s.complete === 'function') { + s.complete(x) + } + }).catch(function (e) { + throwError(e, s, fatalError) + }) } } SubscribeObserver.prototype.error = function (t, e) { var s = this.subscriber - doDispose(this.fatalError, s, s.error, s.error, this.disposable, e) + var fatalError = this.fatalError + Promise.resolve(this.disposable.dispose()).then(function () { + throwError(e, s, fatalError) + }) } export function Subscription (disposable) { @@ -51,14 +61,14 @@ Subscription.prototype.unsubscribe = function () { this.disposable.dispose() } -function doDispose (fatal, subscriber, complete, error, disposable, x) { - Promise.resolve(disposable.dispose()).then(function () { - if (typeof complete === 'function') { - complete.call(subscriber, x) +function throwError (e1, subscriber, throwError) { + if (typeof subscriber.error === 'function') { + try { + subscriber.error(e1) + } catch (e2) { + throwError(e2) } - }).catch(function (e) { - if (typeof error === 'function') { - error.call(subscriber, e) - } - }).catch(fatal) + } else { + throwError(e1) + } } diff --git a/test/observable/subscribe-test.js b/test/observable/subscribe-test.js index f0e8e5b1..49e1b7f2 100644 --- a/test/observable/subscribe-test.js +++ b/test/observable/subscribe-test.js @@ -172,5 +172,18 @@ describe('SubscribeObserver', function () { assert.same(error, e) }) }) + + it('should not swallow error if handler is not present', () => { + var error = new Error() + + return new Promise(function (resolve) { + var subscriber = {} + + var so = new SubscribeObserver(resolve, subscriber, dispose.empty()) + so.error(0, error) + }).then(function (e) { + assert.same(error, e) + }) + }) }) })