nadworny opened a new issue #3155:
URL: https://github.com/apache/camel-quarkus/issues/3155


   # Description
   I'm using an [Camel REST DSL 
generator](https://github.com/apache/camel/blob/main/tooling/maven/camel-restdsl-swagger-plugin/src/main/docs/camel-restdsl-swagger-plugin.adoc)
 for my service. The generated class looks the following:
   ```java
   public final class GeneratedRoute extends RouteBuilder {
       /**
        * Defines Apache Camel routes using REST DSL fluent API.
        */
       public void configure() {
   
           rest()
                   .post("/test")
                   .id("test")
                   .produces("application/json")
                   .param()
                   .name("body")
                   .type(RestParamType.body)
                   .required(true)
                   .endParam()
                   .to("direct:test");
   
       }
   }
   ```
   
   Now, I would like to handle the exception thrown by this endpoint when I 
send an invalid JSON request.
   
   # Expected behaviour
   The exception processor is catching it and returning a custom error response.
   
   # Actual behaviour
   A `500` is returned without going through the processor. Maybe that's a 
generic camel and `RouteBuilder` problem that I can't catch exceptions that are 
created in a different class like the one which is generated?
   
   # How to reproduce
   Add the above GeneratedRoute to the [rest example 
project](https://github.com/apache/camel-quarkus-examples/tree/main/rest-json/src/main/java/org/acme/rest/json)
   
   Add this ErrorResponse (or any pojo) which is generated also from the 
OpenAPI to the example project: 
https://gist.github.com/nadworny/4f391b2eeab28e94b3b297c1dd174d8c
   
   Add this class to the 
[Routes.java](https://github.com/apache/camel-quarkus-examples/blob/main/rest-json/src/main/java/org/acme/rest/json/Routes.java)
   ```java
   @ApplicationScoped
   class ErrorResultProcessor implements Processor {
   
       @Override
       public void process(Exchange exchange) {
           var error = new ErrorResponse();
           error.setStatus(HTTP_BAD_REQUEST);
           exchange.getMessage().setBody(error);
       }
   
   }
   ```
   
   Add the following code to the `configure()` method in 
   ```java
           onException(Exception.class)
               .handled(true)
               .log(ERROR, "${exception.message}")
               .process(errorResultProcessor)
               .marshal()
               .json();
   
           restConfiguration().bindingMode(RestBindingMode.json);
   
           from("direct:test")
               .process(p -> {
                   om.convertValue(p.getIn().getBody(), Fruit.class);
                   p.getIn().setBody(new Fruit("test", "test"));
               });
   ```
   
   Send an invalid request:
   ```curl
   curl --location --request POST 'localhost:8080/test' \
   --header 'Content-Type: application/json' \
   --data-raw '{
       "name": "test"xxx
   }'
   ```
   
   # Environment
   openjdk version "11.0.11" 2021-04-20
   macos 11.6 (20G165)
   https://github.com/apache/camel-quarkus-examples version: 
b1352cd4c0d2a1b7b17ba73c3aca410f516d42f3


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to