static Single<DatabaseAccount> getDatabaseAccountFromAnyLocationsAsync( URL defaultEndpoint, List<String> locations, Func1<URL, Single<DatabaseAccount>> getDatabaseAccountFn) { return getDatabaseAccountFn.call(defaultEndpoint).onErrorResumeNext( e -> { logger.error("Fail to reach global gateway [{}], [{}]", defaultEndpoint, e.getMessage()); if (locations.isEmpty()) { return Single.error(e); } rx.Observable<rx.Observable<DatabaseAccount>> obs = rx.Observable.range(0, locations.size()) .map(index -> getDatabaseAccountFn.call(LocationHelper.getLocationEndpoint(defaultEndpoint, locations.get(index))).toObservable()); // iterate and get the database account from the first non failure, otherwise get the last error. rx.Observable<DatabaseAccount> res = rx.Observable.concatDelayError(obs).first().single(); return res.toSingle().doOnError( innerE -> { logger.error("Fail to reach location any of locations", String.join(",", locations), innerE.getMessage()); }); }); }
/** * Check if the given URI exists by sending an empty request * @param uri uri to check * @return an observable emitting <code>true</code> if the request * was successful or <code>false</code> otherwise */ private Single<Boolean> exists(String uri) { return client.performRequest(HttpMethod.HEAD, uri) .map(o -> true) .onErrorResumeNext(t -> { if (t instanceof HttpException && ((HttpException)t).getStatusCode() == 404) { return Single.just(false); } return Single.error(t); }); }
.flatMap(crs -> merger.merge(crs, p.getLeft(), out) .onErrorResumeNext(t -> { if (t instanceof IllegalStateException) {
private Single<DocumentCollection> ResolveByPartitionKeyRangeIdentityAsync(PartitionKeyRangeIdentity partitionKeyRangeIdentity) { // if request is targeted at specific partition using x-ms-documentd-partitionkeyrangeid header, // which contains value "<collectionrid>,<partitionkeyrangeid>", then resolve to collection rid in this header. if (partitionKeyRangeIdentity != null && partitionKeyRangeIdentity.getCollectionRid() != null) { return this.resolveByRidAsync(partitionKeyRangeIdentity.getCollectionRid()) .onErrorResumeNext(e -> { if (e instanceof NotFoundException) { // This is signal to the upper logic either to refresh // collection cache and retry. return Single.error(new InvalidPartitionException(RMResources.InvalidDocumentCollection)); } return Single.error(e); }); } return Single.just(null); }
@Override public Completable call(FlowContext context) { Operation operation = new Operation.Builder(address, READ_RESOURCE_OPERATION).build(); return dispatcher.execute(operation) .doOnSuccess(result -> context.push(200)) .onErrorResumeNext(throwable -> { if (throwable instanceof DispatchFailure) { context.push(404); return Single.just(new ModelNode()); } else { return Single.error(throwable); } }) .toCompletable(); } }
@Test public void test() throws Exception { Observable.fromCallable(() -> { throw new IOException(); }) .toSingle() .subscribeOn(Schedulers.computation()) .toObservable() .toSingle() .onErrorResumeNext(v -> Single.just(1)) .subscribe(System.out::println, Throwable::printStackTrace); Thread.sleep(1000); }
.onErrorResumeNext(new Func1<Throwable, Single<? extends ChannelFuture>>() { @Override public Single<? extends ChannelFuture> call(Throwable throwable) {
.onErrorResumeNext(new Func1<Throwable, Single<? extends ChannelFuture>>() { @Override public Single<? extends ChannelFuture> call(Throwable throwable) {
@Override public Single<T> call(Single<T> upstream) { return sqlConnection.rxSetAutoCommit(false).toCompletable() .andThen(upstream) .flatMap(item -> sqlConnection.rxCommit().toCompletable().andThen(Single.just(item))) .onErrorResumeNext(throwable -> { return sqlConnection.rxRollback().toCompletable().onErrorComplete() .andThen(sqlConnection.rxSetAutoCommit(true).toCompletable().onErrorComplete()) .andThen(Single.error(throwable)); }).flatMap(item -> sqlConnection.rxSetAutoCommit(true).toCompletable().andThen(Single.just(item))); } }
@Override public Single<T> call(Single<T> upstream) { return sqlConnection.rxSetAutoCommit(false).toCompletable() .andThen(upstream) .flatMap(item -> sqlConnection.rxCommit().toCompletable().andThen(Single.just(item))) .onErrorResumeNext(throwable -> { return sqlConnection.rxRollback().toCompletable().onErrorComplete() .andThen(sqlConnection.rxSetAutoCommit(true).toCompletable().onErrorComplete()) .andThen(Single.error(throwable)); }).flatMap(item -> sqlConnection.rxSetAutoCommit(true).toCompletable().andThen(Single.just(item))); } }