private Observable<Entry> get(SelectBuilder builder) { if (isError) { return Observable.empty(); } else { return builder.get(new ResultSetMapper<Entry>() { @Override public Entry apply(@Nonnull ResultSet rs) throws SQLException { byte[] uncompressed; try { final byte[] compressedTileData = rs.getBytes("tile_data"); uncompressed = CompressUtil.getUncompressedFromGzip(compressedTileData); } catch (final IOException ex) { throw Exceptions.propagate(ex); } return new Entry(rs.getInt("zoom_level"), rs.getInt("tile_column"), flipY(rs.getInt("tile_row"), rs.getInt("zoom_level")), uncompressed); } }).toObservable(); } }
@Override public void onComplete(@NonNull Task<Void> task) { if (isDisposed()) return; if (!task.isSuccessful()) { Exception exception = task.getException(); if (terminated) { RxJavaPlugins.onError(exception); } else { try { terminated = true; observer.onError(exception); } catch (Throwable t) { Exceptions.throwIfFatal(t); RxJavaPlugins.onError(new CompositeException(task.getException(), t)); } } } }
@Override public void onResponse(Call<T> call, Response<T> response) { if (call.isCanceled()) return; try { observer.onNext(response); if (!call.isCanceled()) { terminated = true; observer.onComplete(); } } catch (Throwable t) { if (terminated) { RxJavaPlugins.onError(t); } else if (!call.isCanceled()) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } } }
@Test public void bodyThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingMaybeObserver<String> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.body().subscribe(new ForwardingObserver<String>(observer) { @Override public void onSuccess(String value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Override public Observable<Metadata> getMetadata() { return Observable.defer(() -> { final File metadata = new File(directory, "config.json"); try { if (metadata.exists()) { final String raw = FileUtils.readFileToString(metadata, "UTF-8"); final Metadata result = new Metadata.Builder().setTileJson(raw).build(); return Observable.just(result); } } catch (final IOException ex) { throw Exceptions.propagate(ex); } return Observable.empty(); }); }
@Test public void resultThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingMaybeObserver<Result<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.result().subscribe(new ForwardingObserver<Result<String>>(observer) { @Override public void onSuccess(Result<String> value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void throwingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> errorRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!errorRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); // Don't swallow secondary errors! } } }); RecordingCompletableObserver observer = observerRule.create(); final RuntimeException e = new RuntimeException(); service.completable().subscribe(new ForwardingCompletableObserver(observer) { @Override public void onComplete() { throw e; } }); assertThat(errorRef.get()).isSameAs(e); }
@Test public void bodyThrowingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingObserver<String> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.body().subscribe(new ForwardingObserver<String>(observer) { @Override public void onComplete() { throw e; } }); observer.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void responseThrowingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingObserver<Response<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.response().subscribe(new ForwardingObserver<Response<String>>(observer) { @Override public void onComplete() { throw e; } }); observer.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void resultThrowingInOnCompletedDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingObserver<Result<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.result().subscribe(new ForwardingObserver<Result<String>>(observer) { @Override public void onComplete() { throw e; } }); observer.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void throwingInOnCompleteDeliveredToPlugin() throws InterruptedException { server.enqueue(new MockResponse()); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Throwable> errorRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!errorRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); // Don't swallow secondary errors! } latch.countDown(); } }); TestObserver<Void> observer = new TestObserver<>(); final RuntimeException e = new RuntimeException(); service.completable().subscribe(new ForwardingCompletableObserver(observer) { @Override public void onComplete() { throw e; } }); latch.await(1, SECONDS); assertThat(errorRef.get()).isSameAs(e); }
@Test public void bodyThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSingleObserver<String> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.body().subscribe(new ForwardingObserver<String>(observer) { @Override public void onSuccess(String value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void responseThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSingleObserver<Response<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.response().subscribe(new ForwardingObserver<Response<String>>(observer) { @Override public void onSuccess(Response<String> value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void resultThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSingleObserver<Result<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.result().subscribe(new ForwardingObserver<Result<String>>(observer) { @Override public void onSuccess(Result<String> value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void bodyThrowingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSubscriber<String> subscriber = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.body().subscribe(new ForwardingSubscriber<String>(subscriber) { @Override public void onComplete() { throw e; } }); subscriber.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void responseThrowingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSubscriber<Response<String>> subscriber = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.response().subscribe(new ForwardingSubscriber<Response<String>>(subscriber) { @Override public void onComplete() { throw e; } }); subscriber.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void resultThrowingInOnCompletedDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSubscriber<Result<String>> subscriber = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.result().subscribe(new ForwardingSubscriber<Result<String>>(subscriber) { @Override public void onComplete() { throw e; } }); subscriber.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void responseThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingMaybeObserver<Response<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.response().subscribe(new ForwardingObserver<Response<String>>(observer) { @Override public void onSuccess(Response<String> value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
static <T> Function<BaseResponse<T>, BaseResponse<T>> handleExecuteErrors(String... expectedMethods) { if (expectedMethods.length == 0) { throw new IllegalArgumentException("No expected methods found"); } return response -> { if (nonEmpty(response.executeErrors)) { for (Error error : response.executeErrors) { for (String expectedMethod : expectedMethods) { if (expectedMethod.equalsIgnoreCase(error.method)) { throw Exceptions.propagate(new ApiException(error)); } } } } return response; }; }
@Override public void subscribe(ObservableEmitter<T> subscriber) throws Exception { try { T data = execute(); if (!subscriber.isDisposed() && data != null) { subscriber.onNext(data); } } catch (Throwable e) { ViseLog.e(e); Exceptions.throwIfFatal(e); if (!subscriber.isDisposed()) { subscriber.onError(e); } return; } if (!subscriber.isDisposed()) { subscriber.onComplete(); } }
@Override public void subscribe(@NonNull ObservableEmitter<T> subscriber) throws Exception { try { T data = execute(); if (!subscriber.isDisposed()) { subscriber.onNext(data); } } catch (Throwable e) { HttpLog.e(e.getMessage()); if (!subscriber.isDisposed()) { subscriber.onError(e); } Exceptions.throwIfFatal(e); //RxJavaPlugins.onError(e); return; } if (!subscriber.isDisposed()) { subscriber.onComplete(); } }
@Override public void onError(Throwable throwable) { try { Result<R> error = Result.error(throwable); observer.onNext( new ProgressBean<>(-1, -1, error) ); } catch (Throwable t) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } return; } observer.onComplete(); }
@Override public void onNext(Response<R> response) { if (response.isSuccessful()) { if (response.body() != null) { observer.onNext(response.body()); } else { observer.onComplete(); } } else { terminated = true; Throwable t = new HttpException(response); try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } }
@Override public void actionPerformed(ActionEvent e) { Runnable r = run; if (r != null) { try { r.run(); } catch (Throwable ex) { run = null; stop(); remove(this); Exceptions.throwIfFatal(ex); RxJavaPlugins.onError(ex); return; } if (!periodic) { run = null; stop(); remove(this); } } }
@Override public void subscribe(ObservableEmitter<T> subscriber) throws Exception { try { T data = execute(); if (!subscriber.isDisposed() && data != null) { subscriber.onNext(data); } } catch (Throwable e) { Exceptions.throwIfFatal(e); if (!subscriber.isDisposed()) { subscriber.onError(e); } return; } if (!subscriber.isDisposed()) { subscriber.onComplete(); } }
@Override protected void subscribeActual(CompletableObserver s) { CompletableObserver observer; try { observer = ObjectHelper.requireNonNull(s, "Null Observer"); } catch (Throwable e) { Exceptions.throwIfFatal(e); // can't call onError because no way to know if a Disposable has been set or not // can't call onSubscribe because the call might have set a Disposable already RxJavaPlugins.onError(e); NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS"); npe.initCause(e); throw npe; } source.subscribe(new AttachCompletableObserver(observer, this.compositeDisposable)); }
@Override protected void subscribeActual(Subscriber<? super T> s) { Subscriber<? super T> subscriber; try { subscriber = ObjectHelper.requireNonNull(s, "Null Observer"); } catch (Throwable e) { Exceptions.throwIfFatal(e); // can't call onError because no way to know if a Disposable has been set or not // can't call onSubscribe because the call might have set a Disposable already RxJavaPlugins.onError(e); NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS"); npe.initCause(e); throw npe; } source.subscribe(new AttachSingleObserver<>(subscriber, this.compositeDisposable)); }