Skip to content

Commit 25b9d49

Browse files
committed
finalized custom error handler for issue jakesgordon#3
1 parent 724aa0b commit 25b9d49

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,18 @@ Handling Failures
287287

288288
By default, if you try to call an event method that is not allowed in the current state, the
289289
state machine will throw an exception. If you prefer to handle the problem yourself, you can
290-
define a custom `error` handler, which takes a single event name parameter:
290+
define a custom `error` handler:
291291

292292
var fsm = StateMachine.create({
293293
initial: 'green',
294-
error: function(eventName) {
295-
return 'event ' + eventName + ' not allowed in current state ' + this.current;
294+
error: function(eventName, from, to, args, errorCode, errorMessage) {
295+
return 'event ' + eventName + ' was naughty :- ' + errorMessage;
296296
},
297297
events: [
298298
{ name: 'panic', from: 'green', to: 'red' },
299299
{ name: 'calm', from: 'red', to: 'green' },
300300
]});
301-
alert(fsm.calm()); // "event calm not allowed in current state green"
301+
alert(fsm.calm()); // "event calm was naughty :- event not allowed in current state green"
302302

303303
Release Notes
304304
=============

state-machine.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ StateMachine = {
66

77
//---------------------------------------------------------------------------
88

9+
Error: {
10+
INVALID_TRANSITION: 100, // caller tried to fire an event that was innapropriate in the current state
11+
PENDING_TRANSITION: 200, // caller tried to fire an event while an async transition was still pending
12+
INVALID_CALLBACK: 300, // caller provided callback function threw an exception
13+
},
14+
15+
//---------------------------------------------------------------------------
16+
917
create: function(cfg, target) {
1018

1119
var initial = (typeof cfg.initial == 'string') ? { state: cfg.initial } : cfg.initial; // allow for a simple string, or an object with { state: 'foo', event: 'setup', defer: true|false }
@@ -43,7 +51,7 @@ StateMachine = {
4351
fsm.is = function(state) { return this.current == state; };
4452
fsm.can = function(event) { return !!map[event][this.current] && !this.transition; };
4553
fsm.cannot = function(event) { return !this.can(event); };
46-
fsm.error = cfg.error || function(eventName, error) { throw error; };
54+
fsm.error = cfg.error || function(name, from, to, args, error, msg) { throw msg; }; // default behavior when something unexpected happens is to throw an exception, but caller can override this behavior if desired (see github issue #3)
4755

4856
if (initial && !initial.defer)
4957
fsm[initial.event]();
@@ -60,7 +68,7 @@ StateMachine = {
6068
return func.apply(fsm, [name, from, to].concat(args));
6169
}
6270
catch(e) {
63-
fsm.error(name, e);
71+
return fsm.error(name, from, to, args, StateMachine.Error.INVALID_CALLBACK, "an exception occurred in a caller-provided callback function");
6472
}
6573
}
6674
},
@@ -76,10 +84,10 @@ StateMachine = {
7684
return function() {
7785

7886
if (this.transition)
79-
return this.error(name, "event " + name + " inappropriate because previous transition did not complete");
87+
return this.error(name, from, to, args, StateMachine.Error.PENDING_TRANSITION, "event " + name + " inappropriate because previous transition did not complete");
8088

8189
if (this.cannot(name))
82-
return this.error(name, "event " + name + " inappropriate in current state " + this.current);
90+
return this.error(name, from, to, args, StateMachine.Error.INVALID_TRANSITION, "event " + name + " inappropriate in current state " + this.current);
8391

8492
var from = this.current;
8593
var to = map[from];

state-machine.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_basics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ test("inappropriate events", function() {
151151
test("inappropriate event handling can be customized", function() {
152152

153153
var fsm = StateMachine.create({
154-
error: function(eventName, message) { return message; }, // return error message instead of throwing an exception
154+
error: function(e, name, from, to, args, msg) { return msg; }, // return error message instead of throwing an exception
155155
initial: 'green',
156156
events: [
157157
{ name: 'warn', from: 'green', to: 'yellow' },

0 commit comments

Comments
 (0)