Propagating exceptions to another RouteBuilder

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

Propagating exceptions to another RouteBuilder

Stig Døssing
Hi,

I would like to use different RouteBuilders to declare my routes. I have a RouteBuilder that sets up some REST endpoints, and forwards requests to routes declared in other RouteBuilders. The REST RouteBuilder sets up some general exception handling to return "nice" HTTP status codes in case of errors.

I have some routes set up as follows:

RestRouteBuilder {
  onException(Exception.class)
    .handled(true)
    .process(return a 500-series code)

  onException(JsonProcessingException.class)
    .handled(true)
    .process(return a 400-series code)

   from(rest)
     .choice()
       .when(the request is type A)
         .to(type A route)
       .otherwise()
         .to(type B route)
}

TypeARouteBuilder {
  onException(SomeRouteSpecificException.class)
     .handled(true)
     .process(handle it in some way)

  from(type A route)
    .process(do something that can throw both route specific and other exception types)
    .to(write to DB route)
}

TypeBRouteBuilder {
  onException(SomeOtherRouteSpecificException.class)
     .handled(true)
     .process(handle it in some way)

  from(type B route)
    .process(do something that can throw both route specific and other exception types)
    .to(write to DB route)
}

DBRouteBuilder {
  onException(AuthenticationException.class)
     .redeliveryPolicy(new RedeliveryPolicy().maximumRedeliveries(10))

  from(type A route)
    .to(the DB endpoint)
}

I'd like to be able to set onException clauses in each RouteBuilder, and have uncaught exception types propagate up to the REST route builder's onException clauses.

Others have asked about this before, and have gotten the following suggestions:

  *   Put the exception handling in a common superclass. - This works, but requires me to make a hierarchy of superclasses, and to remember to call super.configure in each RouteBuilder. It also prevents me from varying exception handling for exceptions thrown from DBRouteBuilder depending on whether it was called by TypeARouteBuilder or TypeBRouteBuilder.
  *   Set the error handler to the NoErrorHandler in the TypeA/B and DB route builders, and propagate all exceptions to the REST route builder - This prevents me from setting onException clauses in each RouteBuilder. Best I can do in this case is to use doTry/doCatch in each route in the TypeA/B and DB route builders. This is unpleasant if I have many routes.

The reason I can't get more Java-like exception propagation seems to be that when the DefaultErrorHandler gets an exception it is not configured to handle, it delegates handling to the FatalFallbackErrorHandler. This prevents propagation to a parent RouteBuilder.

Would it be possible to implement a configuration option for the DefaultErrorHandler, that would make it act like the NoErrorHandler if it encounters an exception it isn't configured to handle (i.e. there is no exception policy configured for that type)? Is there a good reason not to have such an option?