Skip to content

Commit 879bcbb

Browse files
committed
add custom error callback explanation to the README
1 parent a4c76be commit 879bcbb

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,22 @@ same as the first example in this section where you simply define your own start
282282

283283
So you have a number of choices available to you when initializing your state machine.
284284

285+
Handling Failures
286+
======================
287+
288+
By default, if you try to call an event method that is not allowed in the current state, the state machine will throw an exception. If you prefer to handle the problem yourself, you can define a custom `error` handler, which takes a single event name parameter:
289+
290+
var fsm = StateMachine.create({
291+
initial: 'green',
292+
error: function(eventName) {
293+
return 'event ' + eventName + ' not allowed in current state ' + this.current;
294+
},
295+
events: [
296+
{ name: 'panic', from: 'green', to: 'red' },
297+
{ name: 'calm', from: 'red', to: 'green' },
298+
]});
299+
alert(fsm.calm()); // "event calm not allowed in current state green"
300+
285301
Release Notes
286302
=============
287303

0 commit comments

Comments
 (0)