Uncaught execution exception - how to work with RxJava

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Uncaught execution exception - how to work with RxJava

This post was updated on .
Having issues trying to use RxJava with Ratpack.

Our business logic lives in a separate module and we import it into our ratpack server module. The business logic module uses RxJava - MigrationService is the class that we import (see code below).

MigrationService throws a NoSuchElementException when it can't find a migration. I've tried to handle this in the following ways (see comments in code):

      ctx.byMethod(method -> method
                .get(() -> {
                    String sector = ctx.getPathTokens().get("sector");

                    // A) same as B, but also get double transmission error
                            .subscribe(ctx::render, e -> ctx.getResponse().send(e.getMessage()));
                    // B) This block logs:
                    // ERROR 2016-10-20T18:40:26,234 ! [ratpack-compute-4-4] r.e.Execution Uncaught execution
                    //      exception java.util.NoSuchElementException: Sequence contains no elements
                    //  RxRatpack.promiseSingle(ctx.get(MigrationService.class).findMigration(sector))
                    //          .map(Jackson::json)
                    //          .onError(NoSuchElementException.class, e -> ctx.getResponse().send(e.getMessage()))
                    //          .then(ctx::render);
                    // C) This bypasses the error handling in the subscriber and goes to the DefaultServerErrorHandler
                    //  RxRatpack.bindExec(ctx.get(MigrationService.class).findMigration(sector))
                    //        .map(Jackson::json)
                    //        // In this case, it returns empty and I throw on empty observable.
                    //        .switchIfEmpty(Observable.error(new NoSuchElementException()))
                    //        .subscribe(ctx::render, e -> ctx.getResponse().send(e.getMessage()));

What is the correct way to handle exceptions when using RxJava and Ratpack and avoid getting the error printed to the logs, or redirected to the default error handlers?

What exactly is the difference between A, B and C?