Skip to content
JordanMartinez edited this page May 12, 2016 · 7 revisions

Note: This page is obsolete. Please see the Home Page

From version 1.2.0, ReactFX includes an error-reporting mechanism. This mechanism is used to report errors thrown by user-provided functions, such as a subscriber, a function passed to map(), etc.

Properties

  • Does not force the user to handle errors.
  • Stream combinators propagate errors.
  • Possible to cut off error propagation by explicitly handling the errors.

Usage

Observe errors

To observe errors of a stream, one uses the monitor(Consumer<Throwable>) method.

EventStream<T> stream = ...;
stream.monitor(error -> handle(error));

Alternatively, one can use the errors() method to obtain a stream of errors reported by an event stream:

EventStream<T> stream = ...;
EventStream<Throwable> errors = stream.errors();

Neither of the above methods consume errors, but errors are propagated to any stream based on the original stream. In the following code:

EventStream<T> stream = ...;
stream.monitor(error -> handle(error));
EventStream<U> mapped = stream.map(t -> f(t));

mapped reports the same errors as stream, plus any errors thrown by its own subscribers or the function f.

To observe both the events and the errors at once, use the two-argument subscribe method:

EventStream<T> stream = ...;
stream.subscribe(evt -> doSomething(evt), err -> handle(err));

From version 2.0, there is the Subscriber interface that handles both events and errors:

EventStream<T> stream = ...;
stream.subscribe(new Subscriber<T>() {
    public void onEvent(T event) { /* handle event */ }
    public void onError(Throwable e) { /* handle error */ }
});

Handle errors

To stop downstream propagation of errors, one uses the handleErrors(Consumer<Throwable>) method:

EventStream<T> stream = ...;
EventStream<T> handled = stream.handleErrors(error -> handle(error));

Here, handled does not propagate any errors reported by stream. It may still report errors thrown by its own subscribers, though.

Materialize errors

materializeErrors() makes errors reported by a stream explicit by turning them into valid events emitted by the returned stream:

EventStream<T> stream = ...;
EventStream<Try<T>> materialized = stream.materializeErrors();
materialized.subscribe(t -> {
    if(t.isSuccess()) {
        doSomething(t.get());
    } else {
        handleException(t.getFailure());
    }
});

materialized does not propagate any errors reported by stream, but may report errors when its own subscribers throw an exception.