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.

Exception handling in .map, .flatMap and other operations

See original GitHub issue

Should Kefir.Stream emit a error in case of thrown exception in .map()?

For example, the following code will fail with an uncaught exception:

function f(x) {
  if (x % 2 === 0) {
    throw Error();
  } else {
    return x;
  }
}
const stream = Kefir.sequentially(1000, [1, 2, 3]);
const mapped = stream.map(f);
mapped.log();

There is a non-obvious way to prevent this by wrapping map callback into try-catch inside flatMap:

function f(x) {
  if (x % 2 === 0) {
    throw Error();
  } else {
    return x;
  }
}
const stream = Kefir.sequentially(1000, [1, 2, 3]);
const mapped = stream.flatMap(function (value) {
  try { return Kefir.constant(f(value)); }
  catch (error) { return Kefir.constantError(error); }
});
mapped.log();

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
mAAdhaTTahcommented, Oct 25, 2017

Since this is foundational to the way Kefir was designed, this is unlikely to change, so I’m going to close this issue.

0reactions
niahoocommented, Sep 12, 2016

Ok, thank you, it works great 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

rxjava - how to handle exception within map() - Stack Overflow
I am transforming the Response object into a LoginSession object using map() operator but there can be an IOException thrown during ...
Read more >
7 Handling errors and exceptions - The Joy of Kotlin
In this chapter, you'll work through a variety of exercises that teach you how to handle errors and exceptions in Kotlin. One skill...
Read more >
Error Handling with Optional in Java | by Satish Thakur | Medium
Prior to Java 8 there were typically two ways to handle errors. Either by throwing exceptions or by returning null from a method....
Read more >
Error handling in RxJava - Dan Lew Codes
Along those same lines, flatMap() (or any other operator that returns an Observable ) can simply return Observable.error() instead of ...
Read more >
Difference Between map() And flatMap() In Java Stream
In Java, the Stream interface has a map() and flatmap() methods and both have intermediate stream operation and return another stream as ...
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