RxJs does not seem to have a variation of catch()
method which allow to capture only certain type of exception. But many times I found myself in a situation where such operator is needed.
For example, imaging I need to parse each line of a log file and print the parsed message. Some lines of the log file might have been corrupted, but I really don't care and just want to output a "Log message corrupted" for such line and continue to the next line.
If catch()
did allow us to specify a specific type of error to catch and pass through all other errors, we can then do some thing like pseudo-code below
readLogs()
.flatMap parseLog
.catchOnly ParseError, () ->
'Log message corrupted'
.subscribe (logMessage) ->
console.log logMessage
I was wondering what would be the proper way of RxJs to do that given the current limitation of catach()
.
You would need to nest
catch
in order to prevent an error from terminating the entire chain and you would need to separate the logic for splitting and parsing lines: