Skip to content

Commit

Permalink
fix(subscribe): Throw fatal if error handler is not present (#477)
Browse files Browse the repository at this point in the history
* Throw error if handler is not present

* Bind context to error handler

* Prefer passing context instead of bind
  • Loading branch information
TrySound authored and briancavalier committed Aug 17, 2017
1 parent ae46a92 commit b3d7278
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/observable/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}
13 changes: 13 additions & 0 deletions test/observable/subscribe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
})

0 comments on commit b3d7278

Please # to comment.