Alex T
Alex T

Reputation: 1693

How can I pass last stream value to error handler?

Let's say I have the following synchronous code that requires original arguments when handling errors:

var input = createInput(123);

try {
  doWork(input);
} catch (err) {
  handleError(input, err); // error handling requires input
}

What is the best way to pass the original arguments to the error hander?

Rx.Observable.just(123).map(createInput).map(doWork).subscribe(
  function(result) {
    // success
  }, 
  function(err) {
    // how can I get the argument of doWork (the result of createInput) here?
  }
);

Upvotes: 2

Views: 251

Answers (1)

Brandon
Brandon

Reputation: 39192

Just add it as a property of the Error object before you throw it or in wrapper code like below.

...map(function (value) {
    try {
        return createInput(value);
    }
    catch (e) {
        e.lastValue = value;
        throw e;
    }
}).subscribe(..., function (e) {
    e.lastValue...

Upvotes: 1

Related Questions