Skip to content

Commit

Permalink
Catch errors in stores
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 14, 2015
1 parent 1409e29 commit 1dfdd75
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 5 deletions.
13 changes: 12 additions & 1 deletion dist/alt-browser-with-addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,18 @@ var AltStore = (function () {
}
if (model[LISTENERS][payload.action]) {
_this8[SET_STATE] = false;
var result = model[LISTENERS][payload.action](payload.data);
var result = false;

try {
result = model[LISTENERS][payload.action](payload.data);
} catch (e) {
if (_this8[LIFECYCLE].error) {
_this8[LIFECYCLE].error(e, payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
} else {
throw e;
}
}

if (result !== false && _this8[SET_STATE] === false) {
_this8.emitChange();
}
Expand Down
13 changes: 12 additions & 1 deletion dist/alt-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,18 @@ var AltStore = (function () {
}
if (model[LISTENERS][payload.action]) {
_this8[SET_STATE] = false;
var result = model[LISTENERS][payload.action](payload.data);
var result = false;

try {
result = model[LISTENERS][payload.action](payload.data);
} catch (e) {
if (_this8[LIFECYCLE].error) {
_this8[LIFECYCLE].error(e, payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
} else {
throw e;
}
}

if (result !== false && _this8[SET_STATE] === false) {
_this8.emitChange();
}
Expand Down
13 changes: 12 additions & 1 deletion dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,18 @@ var AltStore = (function () {
}
if (model[LISTENERS][payload.action]) {
_this8[SET_STATE] = false;
var result = model[LISTENERS][payload.action](payload.data);
var result = false;

try {
result = model[LISTENERS][payload.action](payload.data);
} catch (e) {
if (_this8[LIFECYCLE].error) {
_this8[LIFECYCLE].error(e, payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
} else {
throw e;
}
}

if (result !== false && _this8[SET_STATE] === false) {
_this8.emitChange();
}
Expand Down
13 changes: 12 additions & 1 deletion dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,18 @@ var AltStore = (function () {
}
if (model[LISTENERS][payload.action]) {
_this8[SET_STATE] = false;
var result = model[LISTENERS][payload.action](payload.data);
var result = false;

try {
result = model[LISTENERS][payload.action](payload.data);
} catch (e) {
if (_this8[LIFECYCLE].error) {
_this8[LIFECYCLE].error(e, payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
} else {
throw e;
}
}

if (result !== false && _this8[SET_STATE] === false) {
_this8.emitChange();
}
Expand Down
18 changes: 17 additions & 1 deletion src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,23 @@ class AltStore {
}
if (model[LISTENERS][payload.action]) {
this[SET_STATE] = false
const result = model[LISTENERS][payload.action](payload.data)
let result = false

try {
result = model[LISTENERS][payload.action](payload.data)
} catch (e) {
if (this[LIFECYCLE].error) {
this[LIFECYCLE].error(
e,
payload.action.toString(),
payload.data,
this[STATE_CONTAINER]
)
} else {
throw e
}
}

if (result !== false && this[SET_STATE] === false) {
this.emitChange()
}
Expand Down
95 changes: 95 additions & 0 deletions test/failed-dispatch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { assert } from 'chai'
import Alt from '../dist/alt-with-runtime'
import sinon from 'sinon'

export default {
'catch failed dispatches': {
'uncaught dispatches result in an error'() {
const alt = new Alt()
const actions = alt.generateActions('fire')

class Uncaught {
constructor() {
this.bindListeners({ fire: actions.FIRE })
}

fire() {
throw new Error('oops')
}
}

const uncaught = alt.createStore(Uncaught)

assert.throws(() => actions.fire())
},

'errors can be caught though'() {
const alt = new Alt()
const actions = alt.generateActions('fire')

class Caught {
constructor() {
this.x = 0
this.bindListeners({ fire: actions.FIRE })

this.on('error', () => {
this.x = 1
})
}

fire() {
throw new Error('oops')
}
}

const caught = alt.createStore(Caught)

const storeListener = sinon.spy()

caught.listen(storeListener)

assert(caught.getState().x === 0)
assert.doesNotThrow(() => actions.fire())
assert(caught.getState().x === 1)

assert.notOk(storeListener.calledOnce, 'the store did not emit a change')

caught.unlisten(storeListener)
},

'you have to emit changes yourself'() {
const alt = new Alt()
const actions = alt.generateActions('fire')

class CaughtReturn {
constructor() {
this.x = 0
this.bindListeners({ fire: actions.FIRE })

this.on('error', () => {
this.x = 1
this.emitChange()
})
}

fire() {
throw new Error('oops')
}
}

const caughtReturn = alt.createStore(CaughtReturn)

const storeListener = sinon.spy()

caughtReturn.listen(storeListener)

assert(caughtReturn.getState().x === 0)
assert.doesNotThrow(() => actions.fire())
assert(caughtReturn.getState().x === 1)

assert.ok(storeListener.calledOnce)

caughtReturn.unlisten(storeListener)
},
}
}

0 comments on commit 1dfdd75

Please sign in to comment.