-
Notifications
You must be signed in to change notification settings - Fork 27
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
Add generic error handler that can potentially be overriden by librar… #51
Conversation
…y user to provide custom error handling logic
Thanks for bringing this out! The error handling is a delicate topic in the context of promises, and especially so when having promise chains. Can you provide a bit more context in how you use it? The PR is just aggregating the error throwing into a single place, which is certainly of value, but are you doing something more than that?
|
Yes, when initializing the state machine I am providing my own error I.e:
|
@@ -59,20 +59,23 @@ var StateMachine = stampit({ | |||
}, | |||
methods: { | |||
emit: _.noop, | |||
error: function(msg, option) { | |||
throw new this.factory.FsmError(msg, option); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR in the current state extends the configuration object with a new property error
.
What about providing the error
as part of the callbacks
:
var fsm = StateMachine({
initial: 'red',
events: [
{ name: 'red', from: 'green', to: 'red' }
],
callbacks: {
error: function(msg, option) {
throw new Error('my error');
}
}
});
in which case the above
throw new this.factory.FsmError(msg, option);
becomes:
if (this.callbacks.error) {
return this.callbacks.error(msg, option);
} else {
throw new this.factory.FsmError(msg, option);
}
I like the idea of being able to replace the default error handling but I'm not sure this way is the most appropriate one. Right now these are the ways to customise this behaviour: Configure the StateMachine libraryStateMachine.errorHandler = (msg, option) => {
//my handler
} advantages:
disadvantages:
Extend the configuration objectvar fsm = StateMachine({
initial: 'here',
events: [
{ name: 'jump', from: 'here', to: 'there' }
],
error: myErrorHandler,
}); advantages:disadvantages:
Provide s callbackvar fsm = StateMachine({
initial: 'red',
events: [
{ name: 'red', from: 'green', to: 'red' }
],
callbacks: {
error: function(msg, option) {
throw new Error('my error');
}
}
}); advantages:
disadvantages:
|
Extend the configuration objectThe idea of having the custom error handler in the configurationn follows the same style as jakesgordon fsm: https://github.com/jakesgordon/javascript-state-machine#handling-failures I dont see why providing this in the config would be a disadvantage? As in my opinion this is a actually a configuration property. The other options are worth evaluating too, is good that you brought them up. Providing the error function as a callback:The error handler is actually not a callback related to the events per se. Wouldn't we be overloading the definition of callbacks here? Also as you said it could have some collisions... Configure the StateMachine libraryThis could be an option, although it would be nice if each instance can define its own error handler? I would say that this option is not exclusive of the other two. We can have a general error handler in case we want to define one for all instances and a instance specific handler.? |
Extending the configuration object is the option that encapsulates quite well the customisation of the euro handling to a specific fsm instance. However, the PR leverages too well the current library implementation. It is worth mentioning that this is quite accidental as the current implementation is somewhere midway from the pre-stamp-it implementation to a desired state where we leverage fully composability features enabled by stamp-it (see #17 for wanted API features). Let me digest a bit how to move this forward. I don't want to move too fast without careful consideration on impact on evolution. |
Been thinking about a way forward that brings together StateMachine({
initial: 'start',
final: 'end',
events: [
{ name: 'go', from: 'start', to: 'middle' },
{ name: 'further', from: 'middle', to: 'end' }
],
callbacks: {
},
ops: {
callbackPrefix,
error,
Promise
}
}); In the example above the |
This PR is a suggestion to deal with #14. With it users of the library can provide their own error handling logic by overriding the error function.
In our case this PR allowed us to define a custom handler when you throw Exceptions like "Invalid event in current state", and then we can translate that Exception to one of our own. Thus we define this in a central place instead of needing to catch every time we call any event, and do the proper translation.
What do you think?