You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/10/05 08:50:33 UTC

[GitHub] [camel-quarkus] nadworny opened a new issue #3155: JsonParseException not caught when using OpenAPI generated REST DSL

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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-quarkus] davsclaus commented on issue #3155: JsonParseException not caught when using OpenAPI generated REST DSL

Posted by GitBox <gi...@apache.org>.
davsclaus commented on issue #3155:
URL: https://github.com/apache/camel-quarkus/issues/3155#issuecomment-968351968


   That is expected as the 2 route builders are not sharing onException.
   
   In Camel 3.12 onwards you can use route configurations to share error handling to all route builder classes
   https://camel.apache.org/manual/route-configuration.html
   


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-quarkus] nadworny commented on issue #3155: JsonParseException not caught when using OpenAPI generated REST DSL

Posted by GitBox <gi...@apache.org>.
nadworny commented on issue #3155:
URL: https://github.com/apache/camel-quarkus/issues/3155#issuecomment-937660380


   Thanks for a suggestion @jamesnetherton . Unfortunately after applying it, nothing changed. Probably I forgot to mention but once the `GeneratedRoute` is within the same configure as the `onException`, everything works as expected.


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-quarkus] jamesnetherton commented on issue #3155: JsonParseException not caught when using OpenAPI generated REST DSL

Posted by GitBox <gi...@apache.org>.
jamesnetherton commented on issue #3155:
URL: https://github.com/apache/camel-quarkus/issues/3155#issuecomment-937645488


   I think the problem is that in your `onException` handler you are doing:
   
   ```
   .marshal().json();
   ```
   
   Since the JSON `RestBindingMode` is enabled, you don't need to do the marshalling step manually. It will result in Camel attempting to marshal an already marshalled body. Hence the 500 response.


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-quarkus] nadworny edited a comment on issue #3155: JsonParseException not caught when using OpenAPI generated REST DSL

Posted by GitBox <gi...@apache.org>.
nadworny edited a comment on issue #3155:
URL: https://github.com/apache/camel-quarkus/issues/3155#issuecomment-937660380


   Thanks for the suggestion @jamesnetherton . Unfortunately after applying it, nothing changed. Probably I forgot to mention but once the `GeneratedRoute` is within the same configure as the `onException`, everything works as expected.


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-quarkus] nadworny commented on issue #3155: JsonParseException not caught when using OpenAPI generated REST DSL

Posted by GitBox <gi...@apache.org>.
nadworny commented on issue #3155:
URL: https://github.com/apache/camel-quarkus/issues/3155#issuecomment-937660380


   Thanks for a suggestion @jamesnetherton . Unfortunately after applying it, nothing changed. Probably I forgot to mention but once the `GeneratedRoute` is within the same configure as the `onException`, everything works as expected.


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-quarkus] nadworny edited a comment on issue #3155: JsonParseException not caught when using OpenAPI generated REST DSL

Posted by GitBox <gi...@apache.org>.
nadworny edited a comment on issue #3155:
URL: https://github.com/apache/camel-quarkus/issues/3155#issuecomment-937660380


   Thanks for the suggestion @jamesnetherton . Unfortunately after applying it, nothing changed. Probably I forgot to mention but once the `GeneratedRoute` is within the same configure as the `onException`, everything works as expected.


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-quarkus] jamesnetherton commented on issue #3155: JsonParseException not caught when using OpenAPI generated REST DSL

Posted by GitBox <gi...@apache.org>.
jamesnetherton commented on issue #3155:
URL: https://github.com/apache/camel-quarkus/issues/3155#issuecomment-937645488


   I think the problem is that in your `onException` handler you are doing:
   
   ```
   .marshal().json();
   ```
   
   Since the JSON `RestBindingMode` is enabled, you don't need to do the marshalling step manually. It will result in Camel attempting to marshal an already marshalled body. Hence the 500 response.


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org