Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle promise errors using second argument of then() #524

Merged
merged 1 commit into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/microcosm/src/coroutine.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function coroutine(
}

if (isPromise(body)) {
body.then(action.complete).catch(action.error)
body.then(action.complete, action.error)

return
}

Expand Down
48 changes: 26 additions & 22 deletions packages/microcosm/src/observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,28 +263,32 @@ function notifySubscription(subscription: *, type: *, value: *) {
let observer = subscription._observer

// This is what triggers subscription callbacks
switch (type) {
case NEXT:
observer.next(value)
break
case ERROR:
closeSubscription(subscription)
observer.error(value)
break
case COMPLETE:
closeSubscription(subscription)
observer.complete()
break
case CANCEL:
closeSubscription(subscription)
observer.cancel()
break
default:
assert(
false,
`Unrecognized type ${type}. This is an error internal to Microcosm. ` +
`Please file an issue: https://github.com/vigetlabs/microcosm/issues`
)
try {
switch (type) {
case NEXT:
observer.next(value)
break
case ERROR:
closeSubscription(subscription)
observer.error(value)
break
case COMPLETE:
closeSubscription(subscription)
observer.complete()
break
case CANCEL:
closeSubscription(subscription)
observer.cancel()
break
default:
assert(
false,
`Unrecognized type ${type}. This is an error internal to Microcosm. ` +
`Please file an issue: https://github.com/vigetlabs/microcosm/issues`
)
}
} catch (error) {
scheduler()._raise(error)
}

if (subscription.closed) {
Expand Down
4 changes: 1 addition & 3 deletions packages/microcosm/src/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ class Scheduler {
this._errorCallbacks.forEach(callback => callback(error))
this._errorCallbacks.length = 0
} else {
setTimeout(() => {
throw error
}, 0)
throw error
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/microcosm/src/subject.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class Subject extends Observable {
function fromPromise(promise: Promise<*>): Subject {
let subject = new Subject()

promise.then(subject.complete).catch(subject.error)
promise.then(subject.complete, subject.error)

return subject
}
Expand Down
12 changes: 12 additions & 0 deletions packages/microcosm/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ export function asTree(history) {
}

export const delay = n => new Promise(resolve => setTimeout(resolve, n))

export const withUniqueScheduler = () => {
let oldScheduler = global.__MICROCOSM_SCHEDULER__

beforeEach(function() {
global.__MICROCOSM_SCHEDULER__ = null
})

afterEach(function() {
global.__MICROCOSM_SCHEDULER__ = oldScheduler
})
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Microcosm from 'microcosm'
import { Microcosm, scheduler } from 'microcosm'
import { withUniqueScheduler } from '../../helpers'

describe('Promise middleware', function() {
it('completes when a promise resolves', async function() {
Expand Down Expand Up @@ -69,4 +70,50 @@ describe('Promise middleware', function() {
expect(error).toBe('error')
}
})

describe('promise error trapping', () => {
withUniqueScheduler()

it('does not trap errors in domain handlers', async function() {
const repo = new Microcosm()
const onError = jest.fn()

repo.addDomain('test', {
register() {
return {
action: () => {
throw 'Oops'
}
}
}
})

scheduler().onError(onError)

await repo.push('action')

expect(onError).toHaveBeenCalledWith('Oops')
})

it('does not trap errors in effect handlers', async function() {
const repo = new Microcosm()
const onError = jest.fn()

repo.addEffect({
register() {
return {
action: () => {
throw 'Oops'
}
}
}
})

scheduler().onError(onError)

await repo.push('action')

expect(onError).toHaveBeenCalledWith('Oops')
})
})
})