Frage

Sometimes I want to filter out certain errors in a stream. I'd like to write something like this:

stream
  .filterError (error) ->
    error.type is 'foo'

But there is no filterError method.

As an alternative I thought I could use errors().mapError to map the errors into values, filter them, and then map them back into errors. However, I don't see a way to convert a value in a stream into an error.

# Filter only the errors we are interested in
errors = stream.errors()
  .mapError (error) ->
    error
  .filter (error) ->
    ...
  .mapValuesBackIntoErrors() # ?    

The idea is that the stream in question either carries a value or an error. Both represent domain knowledge; the value means the system is in normal operation and the error means we have a domain error. Some of the domain errors are not such that we want to carry them, though, so I wish to filter them out.

War es hilfreich?

Lösung

The alternative works, of course, and you may use a combination of map and mapError to wrap normal values and errors in the style of the Either type in Haskell. For instance

stream.map((value) -> { value }).mapError((error) -> { error })

Now your stream would output something like this:

{ value: 1 }
{ value: 2 }
{ error: "cannot connect to host" }

On the other hand, actually implementing filterError wouldn't be too hard. Consider implementing this yourself and making a PR.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top