flyd: Handle Error cases
Any thoughts on how to handle errors in a stream? For example, if a promise is rejected, from what i saw in code the stream will never be triggered, since the promise only invoke like this n.then(s) //line 134.
I try to improve this, but have not come up a rational solution. Should stream just end itself when it encounters an error, or should it provide a way to be listened to its errors?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 15 (7 by maintainers)
Commits related to this issue
- Implementing error handling based on discussion in issue #35 — committed to datavore-labs/flyd by jplikesbikes 8 years ago
- Implementing error handling as described in issue #35 — committed to jplikesbikes/flyd by jplikesbikes 8 years ago
This is a great question and I’m aware that the documentation doesn’t describe errors at all 😦
My current opinion is that Flyd should not concern itself with error handling. I don’t see any benefit in doing so.
For normal synchronous code developers know how to handle errors. You need to handle that some functions might return
null/undefinedin certain cases and if you’re using functions in a way so they might throw you usetry/catch. Promises offers similar features for asynchronous code.You know all of the above of course. But that Flyd doesn’t do any
n.catch(someErrHandler)might still seem like a problem to you. This is why I don’t think it’s a problem. First consider an example with synchronous error handling:The above is pretty straight forward. We do an operation that might throw
JSON.parse. If it doesn’t throw the stream is set to it’s return value and if it does some default data is used instead. The same thing can be achieved with promises today:Is you’ve properly noticed and mentioned Flyd doesn’t handle rejected promises. The idea is that you should never send promises that might reject down a stream. This is achieved by adding
catchhandlers to your promises before you return them to a stream.The benefits of the above method is:
This is my current thinking and this is what I’ve done until know. But this is not set in stone. If you see any problems with the above approach by all means express your concerns!