Skip to content

Commit

Permalink
Fixed #1480: exception in reaction effects where not handled correctl…
Browse files Browse the repository at this point in the history
…y by the error handling system
  • Loading branch information
mweststrate committed Apr 16, 2018
1 parent 52d1541 commit 8607ec3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/core/reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,17 @@ export class Reaction implements IDerivation, IReactionPublic {
if (shouldCompute(this)) {
this._isTrackPending = true

this.onInvalidate()
if (this._isTrackPending && isSpyEnabled()) {
// onInvalidate didn't trigger track right away..
spyReport({
name: this.name,
type: "scheduled-reaction"
})
try {
this.onInvalidate()
if (this._isTrackPending && isSpyEnabled()) {
// onInvalidate didn't trigger track right away..
spyReport({
name: this.name,
type: "scheduled-reaction"
})
}
} catch (e) {
this.reportExceptionInDerivation(e)
}
}
endBatch()
Expand Down Expand Up @@ -135,6 +139,8 @@ export class Reaction implements IDerivation, IReactionPublic {
return
}

if (globalState.disableErrorBoundaries) throw error

const message = `[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: '${this}`
console.error(message, error)
/** If debugging brought you here, please, read the above message :-). Tnx! */
Expand Down
26 changes: 26 additions & 0 deletions test/base/errorhandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,32 @@ test("it should be possible to handle global errors in reactions", () => {
})
})

test("it should be possible to handle global errors in reactions - 2 - #1480", () => {
utils.supressConsole(() => {
const a = mobx.observable.box(1)
const errors = []
const d2 = mobx.onReactionError(e => errors.push(e))

const d = mobx.reaction(
() => a.get(),
a => {
if (a >= 2) throw a
}
)

a.set(2)
a.set(3)

d2()
a.set(4)

expect(errors).toEqual([2, 3])
d()

checkGlobalState()
})
})

test("global error handling will be skipped when using disableErrorBoundaries - 1", () => {
mobx.configure({ disableErrorBoundaries: true })
try {
Expand Down

0 comments on commit 8607ec3

Please sign in to comment.