Exception handling in .map, .flatMap and other operations
See original GitHub issueShould 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:
- Created 7 years ago
- Comments:15 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Since this is foundational to the way Kefir was designed, this is unlikely to change, so I’m going to close this issue.
Ok, thank you, it works great 😃