How to send an error from a .map() on an xstream Stream?

117 Views Asked by At

Let's say I would like the Stream to emit an error in certain cases:

import xs from 'xstream'
//...
function transformBlah(blah) {
    if (blah.status >= 200 && blah.status < 300) {
        return blah.body
    } else {
        return new Error('I would like to send an error onto the Stream here')
    }
}
const blahTransformed$ = xs.create(new BlahProducer())
                           .map(transformBlah)
1

There are 1 best solutions below

0
On

I have found that raising an exception achieves this in both rxjs and xstream:

import xs from 'xstream'
//...
const blahTransformed$ = xs.create(new BlahProducer())
                           .map(blah => {
                               if (blah.status >= 200 && blah.status < 300) {
                                   return blah.body
                               } else {
                                   throw blah.status 
                               }
                           })

The exception is caught by the Observable library, and an error is emitted on the Stream.