question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle Error cases

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
paldepindcommented, Jun 10, 2015

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/undefined in certain cases and if you’re using functions in a way so they might throw you use try/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:

var userStringStream = flyd.stream(); // user information that should be formatted as JSON
var userData = flyd.map(function(userString) {
  try {
    return JSON.parse(userString);
  } catch (err) {
    return emptyUserData;
  }
}, userStringStream);

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:

var userDataUrl = flyd.stream(); // and URL from which user information can be loaded
var userData = flyd.map(function(url) {
  return fetch(url).catch(function(err) {
    return emptyUserData;
  });
});

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 catch handlers to your promises before you return them to a stream.

The benefits of the above method is:

  • Flyd stays simple
  • One doesn’t have to learn anything new about errors handling
  • All the powerful error handling than one normally uses works well with Flyd

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!

0reactions
squiddlecommented, Jul 19, 2016

could it be useful to provide an error stream like the end stream? So for proper error handling the suggested solution should be using either. The error stream would allow to have error handling for errors in flyd or its modules (which should not handle Either so they must handle all errors).

i like the decision to move the usage of either out of the scope of flyd. Even though it probably means every own function (eg. a map fn) which just needs to pass errors along, would need to be wrapped with a curried fn of Either.chain(func, either) (if the func takes the values in the stream) or flyd.map(Either.chain(func)) (if the func takes the streams).

Read more comments on GitHub >

github_iconTop Results From Across the Web

4. Error Handling - Open Book Project
In cases like this, special error objects are raised. These always have a message property containing a description of the problem. You can...
Read more >
Error handling - Rust By Example
Error handling is the process of handling the possibility of failure. For example, failing to read a file and then continuing to use...
Read more >
What is Exception Handling? - SearchSoftwareQuality
Examples of exception handling. The following are examples of exceptions: SQLException is a checked exception that occurs while executing queries on a database ......
Read more >
Control flow and error handling - JavaScript - MDN Web Docs
Exception handling statements. You can throw exceptions using the throw statement and handle them using the try...catch statements.
Read more >
Error monitoring and exception handling in large-scale ...
A solution to handle unhandled exceptions with error monitoring · : Prioritize errors/exceptions found on production servers over those found on ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found