Java Code Examples for rx.exceptions.Exceptions#throwIfFatal()

The following examples show how to use rx.exceptions.Exceptions#throwIfFatal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyRxJavaCallAdapterFactory.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
@Override
public void request(long n) {
    if (n < 0) throw new IllegalArgumentException("n < 0: " + n);
    if (n == 0) return; // Nothing to do when requesting 0.
    if (!compareAndSet(false, true)) return; // Request was already triggered.

    try {
        Response<T> response = call.execute();
        if (!subscriber.isUnsubscribed()) {
            subscriber.onNext(response);
        }
    } catch (Throwable t) {
        Exceptions.throwIfFatal(t);
        if (!subscriber.isUnsubscribed()) {
            subscriber.onError(t);
        }
        return;
    }

    if (!subscriber.isUnsubscribed()) {
        subscriber.onCompleted();
    }
}
 
Example 2
Source File: ResourceFlowableMap.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(T t) {
    if (done) {
        ResourceFlowable.releaseItem(t, release);
    } else {
        R v;

        try {
            v = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null value");
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            upstream.cancel();
            ResourceFlowable.releaseItem(t, release);
            done = true;
            actual.onError(ex);
            return;
        }

        ResourceFlowable.releaseItem(t, release);

        actual.onNext(v);
    }
}
 
Example 3
Source File: Observable.java    From letv with Apache License 2.0 6 votes vote down vote up
static <T> Subscription subscribe(Subscriber<? super T> subscriber, Observable<T> observable) {
    if (subscriber == null) {
        throw new IllegalArgumentException("observer can not be null");
    } else if (observable.onSubscribe == null) {
        throw new IllegalStateException("onSubscribe function can not be null.");
    } else {
        subscriber.onStart();
        if (!(subscriber instanceof SafeSubscriber)) {
            subscriber = new SafeSubscriber(subscriber);
        }
        try {
            hook.onSubscribeStart(observable, observable.onSubscribe).call(subscriber);
            return hook.onSubscribeReturn(subscriber);
        } catch (Throwable e2) {
            Exceptions.throwIfFatal(e2);
            hook.onSubscribeError(new OnErrorFailedException("Error occurred attempting to subscribe [" + e.getMessage() + "] and then again while trying to pass to onError.", e2));
        }
    }
}
 
Example 4
Source File: RxJavaCallAdapterFactory.java    From likequanmintv with Apache License 2.0 6 votes vote down vote up
@Override public void request(long n) {
  if (n < 0) throw new IllegalArgumentException("n < 0: " + n);
  if (n == 0) return; // Nothing to do when requesting 0.
  if (!compareAndSet(false, true)) return; // Request was already triggered.

  try {
    Response<T> response = call.execute();
    if (!subscriber.isUnsubscribed()) {
      subscriber.onNext(response);
    }
  } catch (Throwable t) {
    Exceptions.throwIfFatal(t);
    if (!subscriber.isUnsubscribed()) {
      subscriber.onError(t);
    }
    return;
  }

  if (!subscriber.isUnsubscribed()) {
    subscriber.onCompleted();
  }
}
 
Example 5
Source File: ResourceFlowableToFlowable.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(T t) {
    if (done) {
        ResourceFlowable.releaseItem(t, release);
    } else {
        R v;

        try {
            v = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null value");
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            upstream.cancel();
            ResourceFlowable.releaseItem(t, release);
            done = true;
            actual.onError(ex);
            return;
        }

        ResourceFlowable.releaseItem(t, release);

        actual.onNext(v);
    }
}
 
Example 6
Source File: OperatorFreeze.java    From ferro with Apache License 2.0 6 votes vote down vote up
private void bufferEvent(T event) {
    for (ListIterator<T> it = frozenEventsBuffer.listIterator(); it.hasNext(); ) {
        T frozenEvent = it.next();
        try {
            if (replaceFrozenEventPredicate.call(frozenEvent, event)) {
                it.remove();
            }
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            unsubscribe();
            onError(ex);
            return;
        }
    }
    frozenEventsBuffer.add(event);
}
 
Example 7
Source File: BodyOnSubscribe.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(Response<R> response) {
    if (response.isSuccessful()) {
        subscriber.onNext(response.body());
    } else {
        subscriberTerminated = true;
        Throwable t = new HttpException(response);
        try {
            subscriber.onError(t);
        } catch (OnCompletedFailedException | OnErrorFailedException | OnErrorNotImplementedException e) {
            RxJavaHooks.getOnError().call(e);
        } catch (Throwable inner) {
            Exceptions.throwIfFatal(inner);
            RxJavaHooks.getOnError().call(new CompositeException(t, inner));
        }
    }
}
 
Example 8
Source File: ResultOnSubscribe.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public void onError(Throwable throwable) {
    try {
        subscriber.onNext(Result.<R>error(throwable));
    } catch (Throwable t) {
        try {
            subscriber.onError(t);
        } catch (OnCompletedFailedException | OnErrorFailedException | OnErrorNotImplementedException e) {
            RxJavaHooks.getOnError().call(e);
        } catch (Throwable inner) {
            Exceptions.throwIfFatal(inner);
            RxJavaHooks.getOnError().call(new CompositeException(t, inner));
        }
        return;
    }
    subscriber.onCompleted();
}
 
Example 9
Source File: CallArbiter.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
void emitError(Throwable t) {
    set(STATE_TERMINATED);
    if (!isUnsubscribed()) {
        try {
            subscriber.onError(t);
        } catch (OnCompletedFailedException | OnErrorFailedException | OnErrorNotImplementedException e) {
            RxJavaHooks.getOnError().call(e);
        } catch (Throwable inner) {
            Exceptions.throwIfFatal(inner);
            RxJavaHooks.getOnError().call(new CompositeException(t, inner));
        }
    }
}
 
Example 10
Source File: MesosClient.java    From mesos-rxjava with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(final Throwable e) {
    Exceptions.throwIfFatal(e);
    try {
        delegate.onError(e);
        queue.offer(Optional.of(e));
    } catch (Exception e1) {
        queue.offer(Optional.of(e1));
    }
}
 
Example 11
Source File: ResourceFlowableDefer.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Subscriber<? super T> subscriber) {
    ResourceFlowable<T> rf;

    try {
        rf = call.call();
    } catch (Throwable ex) {
        Exceptions.throwIfFatal(ex);
        EmptySubscription.error(ex, subscriber);
        return;
    }

    rf.subscribe(subscriber);
}
 
Example 12
Source File: OnSubscribeMapLast.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void onCompleted() {
    if (value != EMPTY) {
        T value2;
        try {
            value2 = function.call(value);
        } catch (Throwable e) {
            Exceptions.throwIfFatal(e);
            onError(OnErrorThrowable.addValueAsLastCause(e, value));
            return;
        }
        child.onNext(value2);
    }
    child.onCompleted();
}
 
Example 13
Source File: CallArbiter.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
void emitComplete() {
    set(STATE_TERMINATED);
    try {
        if (!isUnsubscribed()) {
            subscriber.onCompleted();
        }
    } catch (OnCompletedFailedException | OnErrorFailedException | OnErrorNotImplementedException e) {
        RxJavaHooks.getOnError().call(e);
    } catch (Throwable t) {
        Exceptions.throwIfFatal(t);
        RxJavaHooks.getOnError().call(t);
    }
}
 
Example 14
Source File: RateLimitedBatcher.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
/**
 * swallow downstream exceptions and keep the event loop running
 */
private void onNextSafe(Batch<T, I> next) {
    // TODO: capture rate limit exceptions from downstream and apply exponential backoff here too
    try {
        downstream.onNext(next);
    } catch (Throwable ex) {
        Exceptions.throwIfFatal(ex);
        logger.error("onNext failed, ignoring batch {}", next.getIndex(), ex);
    }
}
 
Example 15
Source File: Observable.java    From letv with Apache License 2.0 5 votes vote down vote up
public final Subscription unsafeSubscribe(Subscriber<? super T> subscriber) {
    try {
        subscriber.onStart();
        hook.onSubscribeStart(this, this.onSubscribe).call(subscriber);
        return hook.onSubscribeReturn(subscriber);
    } catch (Throwable e2) {
        Exceptions.throwIfFatal(e2);
        hook.onSubscribeError(new RuntimeException("Error occurred attempting to subscribe [" + e.getMessage() + "] and then again while trying to pass to onError.", e2));
    }
}
 
Example 16
Source File: Completable.java    From letv with Apache License 2.0 5 votes vote down vote up
public final void subscribe(CompletableSubscriber s) {
    requireNonNull(s);
    try {
        this.onSubscribe.call(s);
    } catch (NullPointerException ex) {
        throw ex;
    } catch (Throwable ex2) {
        ERROR_HANDLER.handleError(ex2);
        Exceptions.throwIfFatal(ex2);
        NullPointerException toNpe = toNpe(ex2);
    }
}
 
Example 17
Source File: OperatorBufferToFile.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
void onNext(T t) {
    try {
        if (!queue.offer(t)) {
            onError(new RuntimeException(
                    "could not place item on queue (queue.offer(item) returned false), item= "
                            + t));
            return;
        } else {
            drain();
        }
    } catch (Throwable e) {
        Exceptions.throwIfFatal(e);
        onError(e);
    }
}
 
Example 18
Source File: SinkSubscriber.java    From mesos-rxjava with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(final SinkOperation<Send> op) {
    try {
        final Send toSink = op.getThingToSink();
        createPost.call(toSink)
            .flatMap(httpClient::submit)
            .flatMap(resp -> {
                final HttpResponseStatus status = resp.getStatus();
                final int code = status.code();

                if (code == 202) {
                    /* This is success */
                    return Observable.just(Optional.<MesosException>empty());
                } else {
                    final HttpResponseHeaders headers = resp.getHeaders();
                    return ResponseUtils.attemptToReadErrorResponse(resp)
                        .map(msg -> {
                            final List<Map.Entry<String, String>> entries = headers.entries();
                            final MesosClientErrorContext context = new MesosClientErrorContext(code, msg, entries);
                            MesosException error;
                            if (400 <= code && code < 500) {
                                // client error
                                error = new Mesos4xxException(toSink, context);
                            } else if (500 <= code && code < 600) {
                                // client error
                                error = new Mesos5xxException(toSink, context);
                            } else {
                                // something else that isn't success but not an error as far as http is concerned
                                error = new MesosException(toSink, context);
                            }
                            return Optional.of(error);
                        });
                }
            })
            .observeOn(Rx.compute())
            .subscribe(exception -> {
                if (!exception.isPresent()) {
                    op.onCompleted();
                } else {
                    op.onError(exception.get());
                }
            });
    } catch (Throwable e) {
        Exceptions.throwIfFatal(e);
        op.onError(e);
    }
}
 
Example 19
Source File: SinkSubscriber.java    From mesos-rxjava with Apache License 2.0 4 votes vote down vote up
@Override
public void onError(final Throwable e) {
    Exceptions.throwIfFatal(e);
}
 
Example 20
Source File: OperatorOnErrorResumeNextViaObservable.java    From mantis with Apache License 2.0 4 votes vote down vote up
@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {
    // shared subscription won't work here
    Subscriber<T> s = new Subscriber<T>() {

        private boolean done = false;

        @Override
        public void onNext(T t) {
            if (done) {
                return;
            }
            child.onNext(t);
        }

        @Override
        public void onError(Throwable e) {
            if (done) {
                Exceptions.throwIfFatal(e);
                return;
            }
            done = true;
            RxJavaPlugins.getInstance().getErrorHandler().handleError(e);
            unsubscribe();
            resumeSequence.unsafeSubscribe(child);
        }

        @Override
        public void onCompleted() {
            if (done) {
                return;
            }
            done = true;
            child.onCompleted();
        }

        @Override
        public void setProducer(final Producer producer) {
            child.setProducer(new Producer() {
                @Override
                public void request(long n) {
                    producer.request(n);
                }
            });
        }

    };
    child.add(s);

    return s;
}