Grokbase Groups Camel users June 2015
FAQ
Hi,

In my Rest DSL route, I use the Validate DSL and mvel to validate data as it
goes through the pipeline. If there's a validation exception, I would like
to return back to the client both an HTTP_RESPONSE_CODE 400 as well as the
reason for the error in the body. I'm able to set the HTTP response code but
not sure how I can customize the reason for each validation exception.

For example, in my route below:

@Override
public void configure() throws Exception {
onException(org.apache.camel.processor.validation.PredicateValidationException.class).handled(true).transform().simple("Validation
failed").setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400));
  rest("system")
   .put("cancel-trade")
   .route()
   .validate().mvel("request.body.?extid > 0")
   .to("sql:select id from trades where outid = :#extid
?outputHeader=id&outputType=SelectOne")
   .validate().mvel("request.headers.?id != null")
   .to("mock:bean");

I currently only say "Validation failed" in the body in the case of a
PredicateValdiationException thrown. I would like to return a customized
human readable message though depending on each failure. So if the first
validate fails, I would like the reply to say "External ID must be greater
than 0" and if the second validate fails "Trade not found in trades table".

Is this possible with the Validate DSL?

Thanks



--
View this message in context: http://camel.465427.n5.nabble.com/Validate-DSL-and-message-returned-to-client-tp5768293.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Search Discussions

  • Claus Ibsen at Jun 16, 2015 at 7:19 am
    Hi

    No the validate dsl will use a predicate that returns true or false.
    If it returns false, then an exception is thrown.

    If you want to do some kind of custom error message etc. Then calling
    a bean is simpler.

    But we could consider adding some way of allowing validate dsl to use
    an expression that can generate the exception message.
    On Tue, Jun 16, 2015 at 5:49 AM, Karts wrote:
    Hi,

    In my Rest DSL route, I use the Validate DSL and mvel to validate data as it
    goes through the pipeline. If there's a validation exception, I would like
    to return back to the client both an HTTP_RESPONSE_CODE 400 as well as the
    reason for the error in the body. I'm able to set the HTTP response code but
    not sure how I can customize the reason for each validation exception.

    For example, in my route below:

    @Override
    public void configure() throws Exception {
    onException(org.apache.camel.processor.validation.PredicateValidationException.class).handled(true).transform().simple("Validation
    failed").setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400));
    rest("system")
    .put("cancel-trade")
    .route()
    .validate().mvel("request.body.?extid > 0")
    .to("sql:select id from trades where outid = :#extid
    ?outputHeader=id&outputType=SelectOne")
    .validate().mvel("request.headers.?id != null")
    .to("mock:bean");

    I currently only say "Validation failed" in the body in the case of a
    PredicateValdiationException thrown. I would like to return a customized
    human readable message though depending on each failure. So if the first
    validate fails, I would like the reply to say "External ID must be greater
    than 0" and if the second validate fails "Trade not found in trades table".

    Is this possible with the Validate DSL?

    Thanks



    --
    View this message in context: http://camel.465427.n5.nabble.com/Validate-DSL-and-message-returned-to-client-tp5768293.html
    Sent from the Camel - Users mailing list archive at Nabble.com.


    --
    Claus Ibsen
    -----------------
    Red Hat, Inc.
    Email: cibsen@redhat.com
    Twitter: davsclaus
    Blog: http://davsclaus.com
    Author of Camel in Action: http://www.manning.com/ibsen
    hawtio: http://hawt.io/
    fabric8: http://fabric8.io/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupusers @
categoriescamel
postedJun 16, '15 at 3:49a
activeJun 16, '15 at 7:19a
posts2
users2
websitecamel.apache.org

2 users in discussion

Karts: 1 post Claus Ibsen: 1 post

People

Translate

site design / logo © 2022 Grokbase