Java Code Examples for io.reactivex.Observable.error()

The following are Jave code examples for showing how to use error() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: GitHub   File: SearchEngine.java   Source Code and License Vote up 6 votes
public Observable<List<Product>> searchFor(@NonNull String searchQueryText) {

    if (searchQueryText == null) {
      return Observable.error(new NullPointerException("SearchQueryText == null"));
    }

    if (searchQueryText.length() == 0) {
      return Observable.error(new IllegalArgumentException("SearchQueryTest is blank"));
    }

    return backend.getAllProducts()
        .delay(1000, TimeUnit.MILLISECONDS)
        .flatMap(Observable::fromIterable)
        .filter(product -> isProductMatchingSearchCriteria(product, searchQueryText))
        .toList()
        .toObservable();
  }
 
Example 2
Project: GitHub   File: SearchEngine.java   Source Code and License Vote up 6 votes
public Observable<List<Product>> searchFor(@NonNull String searchQueryText) {

    if (searchQueryText == null) {
      return Observable.error(new NullPointerException("SearchQueryText == null"));
    }

    if (searchQueryText.length() == 0) {
      return Observable.error(new IllegalArgumentException("SearchQueryTest is blank"));
    }

    return backend.getAllProducts()
        .delay(1000, TimeUnit.MILLISECONDS)
        .flatMap(Observable::fromIterable)
        .filter(product -> isProductMatchingSearchCriteria(product, searchQueryText))
        .toList()
        .toObservable();
  }
 
Example 3
Project: AndroidMVPresenter   File: MainPresenter.java   Source Code and License Vote up 6 votes
public MainPresenter(final AuthRepo authRepo) {
    super();
    this.authRepo = authRepo;

    super.setupRetry(
            3,
            new Function<Throwable, Observable<?>>() {
                @Override
                public Observable<?> apply(Throwable throwable) throws Exception {
                    if (throwable instanceof AuthentificationException && ((AuthentificationException) throwable).statusCode == 401) {
                        return authRepo.authentificate().toObservable();
                    } else if (throwable instanceof IOException) {
                        return Observable.timer(3, TimeUnit.SECONDS); //wait 3 seconds before continue
                    }
                    return Observable.error(throwable);
                }
            });
}
 
Example 4
Project: Reactive-Programming-With-Java-9   File: Modern_Testing.java   Source Code and License Vote up 6 votes
@Test
public void test_error() {
	Observable<String> observable = Observable.error(new Exception("We got an Exception"));
	TestObserver<String> testObserver = new TestObserver<>();

	observable.subscribe(testObserver);

	testObserver.assertError(Exception.class);
	testObserver.assertNotComplete();
	testObserver.assertErrorMessage("We got an Exception");
	testObserver.assertFailure(exception -> {
		return exception instanceof Exception;
	});

	testObserver.assertFailureAndMessage(Exception.class, "We got an Exception");
}
 
Example 5
Project: https-github.com-hyb1996-NoRootScriptDroid   File: ScriptOperations.java   Source Code and License Vote up 5 votes
public Observable<String> importSample(Sample sample) {
    try {
        return importFile(sample.name, mContext.getAssets().open(sample.path), PFile.getExtension(sample.path));
    } catch (IOException e) {
        e.printStackTrace();
        showMessage(R.string.text_import_fail);
        return Observable.error(e);
    }
}
 
Example 6
Project: amv-access-api-poc   File: ResponseHelper.java   Source Code and License Vote up 5 votes
public static <T> Observable<T> parse(Response response, Class<T> clazz) {
    try {
        final ResponseBody responseBody = response.body();
        Reader reader = responseBody.charStream();

        try {
            if (!response.isSuccessful()) {
                ErrorResponseDto errorResponseDto = Clients
                        .defaultObjectMapper
                        .fromJson(reader, ErrorResponseDto.class);

                return Observable.error(new AccessApiException(errorResponseDto, null));
            }

            T parsedBody = Clients
                    .defaultObjectMapper
                    .fromJson(reader, clazz);

            return Observable.just(parsedBody);
        } finally {
            Util.closeQuietly(reader);
        }
    } catch (Exception e) {
        return Observable.error(e);
    } finally {
        Util.closeQuietly(response);
    }
}
 
Example 7
Project: Quran   File: CoordinatesModel.java   Source Code and License Vote up 5 votes
public Observable<Pair<Integer, RectF>> getPageCoordinates(Integer... pages) {
  AyahInfoDatabaseHandler database = ayahInfoDatabaseProvider.getAyahInfoHandler();
  if (database == null) {
    return Observable.error(new NoSuchElementException("No AyahInfoDatabaseHandler found!"));
  }

  return Observable.fromArray(pages)
      .map(page -> new Pair<>(page, database.getPageBounds(page)))
      .subscribeOn(Schedulers.computation());
}
 
Example 8
Project: Quran   File: CoordinatesModel.java   Source Code and License Vote up 5 votes
public Observable<Pair<Integer, Map<String, List<AyahBounds>>>> getAyahCoordinates(
    Integer... pages) {
  AyahInfoDatabaseHandler database = ayahInfoDatabaseProvider.getAyahInfoHandler();
  if (database == null) {
    return Observable.error(new NoSuchElementException("No AyahInfoDatabaseHandler found!"));
  }

  return Observable.fromArray(pages)
      .map(page -> new Pair<>(page, database.getVersesBoundsForPage(page)))
      .subscribeOn(Schedulers.computation());
}
 
Example 9
Project: LocGetter   File: LocationGetterImpl.java   Source Code and License Vote up 5 votes
@Override
public Observable<Location> getLatestLocations() {
    if (ContextCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
        && ContextCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        return Observable.just(googleApiClient)
            .subscribeOn(Schedulers.newThread())
            .doOnNext(this::initGoogleApiClient)
            .doOnNext(client -> checkGoogleApiAvailability())
            .map(this::getLocationSettingsStatus)
            .doOnNext(this::checkSettingsStatus)
            .flatMap(integer -> new LocationSniffer().startEmittingLocations());
    }
    return Observable.error(new PermissionException(Manifest.permission.ACCESS_FINE_LOCATION, ACCESS_LOCATION_PERMISSION_RESULT));
}
 
Example 10
Project: Auto.js   File: ScriptOperations.java   Source Code and License Vote up 5 votes
public Observable<String> importSample(SampleFile sample) {
    try {
        return importFile(sample.getSimplifiedName(), sample.openInputStream(), sample.getExtension());
    } catch (IOException e) {
        e.printStackTrace();
        showMessage(R.string.text_import_fail);
        return Observable.error(e);
    }
}
 
Example 11
Project: RxCommand   File: RxCommand.java   Source Code and License Vote up 5 votes
/**
 * If the receiver is enabled, this method will:
 * <p>
 * 1. Invoke the `func` given at the time of creation.
 * 2. Multicast the returned observable.
 * 3. Send the multicasted observable on {@link #executionObservables()}.
 * 4. Subscribe (connect) to the original observable on the main thread.
 *
 * @param input The input value to pass to the receiver's `func`. This may be null.
 * @return the multicasted observable, after subscription. If the receiver is not
 * enabled, returns a observable that will send an error.
 */
@MainThread
public final Observable<T> execute(@Nullable Object input) {
    boolean enabled = mImmediateEnabled.blockingFirst();
    if (!enabled) {
        return Observable.error(new IllegalStateException("The command is disabled and cannot be executed"));
    }
    try {
        Observable<T> observable = mFunc.apply(input);
        if (observable == null) {
            throw new RuntimeException(String.format("null Observable returned from observable func for value %s", input));
        }

        // This means that `executing` and `enabled` will send updated values before
        // the observable actually starts performing work.
        final ConnectableObservable<T> connection = observable
                .subscribeOn(AndroidSchedulers.mainThread())
                .replay();

        mAddedExecutionObservableSubject.onNext(connection);
        connection.connect();
        return connection;
    } catch (Exception e) {
        e.printStackTrace();
        return Observable.error(e);
    }
}
 
Example 12
Project: RxJava2-Android-Sample   File: EmptyNeverThrowExampleActivity.java   Source Code and License Vote up 5 votes
private Observable<String> getThrowObservable() {
    return Observable.error(new Callable<Throwable>() {
        @Override
        public Throwable call() throws Exception {
            return new Throwable("throw error test");
        }
    });
}
 
Example 13
Project: udacity-baking-app-mvvm-rxjava-dagger   File: RecipeRepository.java   Source Code and License Vote up 5 votes
@NonNull
public Observable<Recipe> getRecipe(int id) {
    if (recipeCache == null) {
        return Observable.error(new Throwable("Recipe not found."));
    }

    for (Recipe r : recipeCache) {
        if (r.getId() == id) {
            return Observable.just(r);
        }
    }

    return Observable.error(new Throwable("Recipe not found."));
}
 
Example 14
Project: GitHub   File: HttpResultFunction.java   Source Code and License Vote up 4 votes
@Override
public Observable<T> apply(@NonNull Throwable throwable) throws Exception {
    //打印具体错误
    LogUtils.e("HttpResultFunction:" + throwable);
    return Observable.error(ExceptionEngine.handleException(throwable));
}
 
Example 15
Project: RxEasyHttp   File: HttpResponseFunc.java   Source Code and License Vote up 4 votes
@Override
public Observable<T> apply(@NonNull Throwable throwable) throws Exception {
    return Observable.error(ApiException.handleException(throwable));
}
 
Example 16
Project: Reactive-Programming-With-Java-9   File: DemoObservable_error.java   Source Code and License Vote up 4 votes
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Observable<String> observable = Observable.error(new Exception("We got an Exception"));
	observable.subscribe(item -> System.out.println("we got" + item), 
			error -> System.out.print(error));
}
 
Example 17
Project: HutHelper   File: HttpResultFunction.java   Source Code and License Vote up 4 votes
@Override
public Observable<T> apply(@NonNull Throwable throwable) throws Exception {
    LogUtils.d(TAG, TAG + " " + throwable.toString());
    return Observable.error(ExceptionEngine.handleException(throwable));
}
 
Example 18
Project: XDroid-Databinding   File: ServerResultErrorFunc.java   Source Code and License Vote up 4 votes
@Override
public Observable<T> apply(Throwable throwable) throws Exception {
    return Observable.error(handleException() == null ? ExceptionEngine.handleException(throwable) : handleException());
}
 
Example 19
Project: basicapp   File: HttpResponseFunc.java   Source Code and License Vote up 4 votes
@Override
public Observable<T> apply(@NonNull Throwable throwable) throws Exception {
    return Observable.error(ExceptionHandle.handleException(throwable));
}