You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Borut Bolčina <bo...@gmail.com> on 2012/03/28 08:44:54 UTC

Printing out bean validation exception message causes, well, exception

Hello,

I am using bean-validator component to validate the content of some fields:

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
 return new RouteBuilder() {
@Override
public void configure() throws Exception {
 DataFormat jaxbDataFormat = new
JaxbDataFormat("com.mycompany.model.entities.weather");
 onException(BeanValidationException.class)
.handled(true)
.process(new Processor() {
 public void process(Exchange exchange) throws Exception {
String station = ((WeatherCurrent) exchange.getIn().getBody()).getStation();
 System.out.println("Weather station '" + station + "' does not meet
validation constraints. Skipping.");
}
 });

from("file:src/test/resources/weather/observation?fileName=observation_si_latest.xml&noop=true")
 .split()
.tokenizeXML("metData")
.unmarshal(jaxbDataFormat)
 .to("bean-validator://x")
.to("mock:meteo");
 }
};
}

Running this test route nicely prints out the weather station which do not
meet the validation criteria:

Weather station 'Bilje pri Novi Gorici' does not meet validation
constraints. Skipping.
Weather station 'Lisca' does not meet validation constraints. Skipping.
Weather station 'Novo mesto' does not meet validation constraints. Skipping.

Now I wanted to print the cause of the validation error, so I added the
exception message in the process method:

String exceptionMessage = exchange.getException().getMessage();
System.out.println("Weather station '" + station + "' does not meet
validation constraints(" + exceptionMessage + "). Skipping.");

and suddenly hell broke loose

[2012/03/28 08:37:48.648] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
Exception occurred while trying to handle previously thrown exception on
exchangeId: ID-BOBB-54461-1332916666871-0-3 using:
[Channel[Wrap[com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7]
->
com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7]].
The previous and the new exception will be logged in the following.
[2012/03/28 08:37:48.653] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
\--> Previous exception on exchangeId: ID-BOBB-54461-1332916666871-0-3
org.apache.camel.component.bean.validator.BeanValidationException:
Validation failed for:
com.mycompany.model.entities.weather.WeatherCurrent@1a4c5b4 errors:
[property: conditions; value: jasna; constraint: must match "jasno|pretežno
jasno|rahlo oblačno|delno oblačno|zmerno oblačno|pretežno
oblačno|oblačno|megla"; ]. Exchange[null] at
org.apache.camel.component.bean.validator.BeanValidator.process(BeanValidator.java:54)
~[camel-bean-validator-2.9.1.jar:2.9.1] at
org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:101)
~[camel-core-2.9.1.jar:2.9.1]
...
FAILED: testNumberOfWeatherStations
java.lang.AssertionError: mock://meteo Received message count. Expected:
<6> but was: <12>
...


What gives?

-borut

Re: Printing out bean validation exception message causes, well, exception

Posted by Borut Bolčina <bo...@gmail.com>.
Thanks Claus, you rock!

-borut

Dne 28. marec 2012 09:05 je Claus Ibsen <cl...@gmail.com> napisal/-a:

> Hi
>
> You handle the exception, so the getException will return null, and
> therefore you cause a 2nd exception to occur.
>
> The caused exception is stored as a property on the exchange.
> Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
> Exception.class);
>
>
>
> On Wed, Mar 28, 2012 at 8:44 AM, Borut Bolčina <bo...@gmail.com>
> wrote:
> > Hello,
> >
> > I am using bean-validator component to validate the content of some
> fields:
> >
> > @Override
> > protected RouteBuilder createRouteBuilder() throws Exception {
> >  return new RouteBuilder() {
> > @Override
> > public void configure() throws Exception {
> >  DataFormat jaxbDataFormat = new
> > JaxbDataFormat("com.mycompany.model.entities.weather");
> >  onException(BeanValidationException.class)
> > .handled(true)
> > .process(new Processor() {
> >  public void process(Exchange exchange) throws Exception {
> > String station = ((WeatherCurrent)
> exchange.getIn().getBody()).getStation();
> >  System.out.println("Weather station '" + station + "' does not meet
> > validation constraints. Skipping.");
> > }
> >  });
> >
> >
> from("file:src/test/resources/weather/observation?fileName=observation_si_latest.xml&noop=true")
> >  .split()
> > .tokenizeXML("metData")
> > .unmarshal(jaxbDataFormat)
> >  .to("bean-validator://x")
> > .to("mock:meteo");
> >  }
> > };
> > }
> >
> > Running this test route nicely prints out the weather station which do
> not
> > meet the validation criteria:
> >
> > Weather station 'Bilje pri Novi Gorici' does not meet validation
> > constraints. Skipping.
> > Weather station 'Lisca' does not meet validation constraints. Skipping.
> > Weather station 'Novo mesto' does not meet validation constraints.
> Skipping.
> >
> > Now I wanted to print the cause of the validation error, so I added the
> > exception message in the process method:
> >
> > String exceptionMessage = exchange.getException().getMessage();
> > System.out.println("Weather station '" + station + "' does not meet
> > validation constraints(" + exceptionMessage + "). Skipping.");
> >
> > and suddenly hell broke loose
> >
> > [2012/03/28 08:37:48.648] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
> > Exception occurred while trying to handle previously thrown exception on
> > exchangeId: ID-BOBB-54461-1332916666871-0-3 using:
> >
> [Channel[Wrap[com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7
> ]
> > ->
> >
> com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7
> ]].
> > The previous and the new exception will be logged in the following.
> > [2012/03/28 08:37:48.653] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
> > \--> Previous exception on exchangeId: ID-BOBB-54461-1332916666871-0-3
> > org.apache.camel.component.bean.validator.BeanValidationException:
> > Validation failed for:
> > com.mycompany.model.entities.weather.WeatherCurrent@1a4c5b4 errors:
> > [property: conditions; value: jasna; constraint: must match
> "jasno|pretežno
> > jasno|rahlo oblačno|delno oblačno|zmerno oblačno|pretežno
> > oblačno|oblačno|megla"; ]. Exchange[null] at
> >
> org.apache.camel.component.bean.validator.BeanValidator.process(BeanValidator.java:54)
> > ~[camel-bean-validator-2.9.1.jar:2.9.1] at
> >
> org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:101)
> > ~[camel-core-2.9.1.jar:2.9.1]
> > ...
> > FAILED: testNumberOfWeatherStations
> > java.lang.AssertionError: mock://meteo Received message count. Expected:
> > <6> but was: <12>
> > ...
> >
> >
> > What gives?
> >
> > -borut
>
>
>
> --
> Claus Ibsen
> -----------------
> CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>

Re: Printing out bean validation exception message causes, well, exception

Posted by Borut Bolčina <bo...@gmail.com>.
I really can't describe the awesome felling when I can remove 7 lines of
code and replace it with one!

onException(BeanValidationException.class)
.handled(true)
 .log("Weather station '${body.station}' does not meet validation
constraints ${exception.message}. Skipping.");

Camel feels more and more concise, great.

-borut

Dne 28. marec 2012 13:17 je Claus Ibsen <cl...@gmail.com> napisal/-a:

> See the simple language.
> http://camel.apache.org/simple
>
> Or try some of the scripting languages like groovy. But simple ought
> to do what you want to do.
>
>
> On Wed, Mar 28, 2012 at 12:05 PM, Borut Bolčina <bo...@gmail.com>
> wrote:
> > Another thing,
> >
> > is there a way to get hold of current exchange in the onException chain?
> >
> > I would like to replace the .proccess with .log
> >
> > onException(BeanValidationException.class)
> >  .handled(true)
> > .process(new Processor() {
> > public void process(Exchange exchange) throws Exception {
> >  String station = ((WeatherCurrent)
> > exchange.getIn().getBody()).getStation();
> > Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
> > Exception.class);
> >  System.out.println("Weather station '" + station + "' does not meet
> > validation constraints(" + cause.getMessage() + "). Skipping.");
> >  }
> > });
> >
> > So instead of using the inline Processor, something like:
> >
> > onException(BeanValidationException.class)
> > .handled(true)
> > .log("Weather station '" + ((WeatherCurrent)
> > exchange.getIn().getBody()).getStation() + "' does not meet validation
> > constraints(" + cause.getMessage() + "). Skipping.");
> >  I know I can create a bean and pass the Exchange as parameter and do the
> > logging there, but I am seeking even leaner solution.
> >
> > -borut
> >
> >
> > Dne 28. marec 2012 09:05 je Claus Ibsen <cl...@gmail.com>
> napisal/-a:
> >
> >> Hi
> >>
> >> You handle the exception, so the getException will return null, and
> >> therefore you cause a 2nd exception to occur.
> >>
> >> The caused exception is stored as a property on the exchange.
> >> Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
> >> Exception.class);
> >>
> >>
> >>
> >> On Wed, Mar 28, 2012 at 8:44 AM, Borut Bolčina <borut.bolcina@gmail.com
> >
> >> wrote:
> >> > Hello,
> >> >
> >> > I am using bean-validator component to validate the content of some
> >> fields:
> >> >
> >> > @Override
> >> > protected RouteBuilder createRouteBuilder() throws Exception {
> >> >  return new RouteBuilder() {
> >> > @Override
> >> > public void configure() throws Exception {
> >> >  DataFormat jaxbDataFormat = new
> >> > JaxbDataFormat("com.mycompany.model.entities.weather");
> >> >  onException(BeanValidationException.class)
> >> > .handled(true)
> >> > .process(new Processor() {
> >> >  public void process(Exchange exchange) throws Exception {
> >> > String station = ((WeatherCurrent)
> >> exchange.getIn().getBody()).getStation();
> >> >  System.out.println("Weather station '" + station + "' does not meet
> >> > validation constraints. Skipping.");
> >> > }
> >> >  });
> >> >
> >> >
> >>
> from("file:src/test/resources/weather/observation?fileName=observation_si_latest.xml&noop=true")
> >> >  .split()
> >> > .tokenizeXML("metData")
> >> > .unmarshal(jaxbDataFormat)
> >> >  .to("bean-validator://x")
> >> > .to("mock:meteo");
> >> >  }
> >> > };
> >> > }
> >> >
> >> > Running this test route nicely prints out the weather station which do
> >> not
> >> > meet the validation criteria:
> >> >
> >> > Weather station 'Bilje pri Novi Gorici' does not meet validation
> >> > constraints. Skipping.
> >> > Weather station 'Lisca' does not meet validation constraints.
> Skipping.
> >> > Weather station 'Novo mesto' does not meet validation constraints.
> >> Skipping.
> >> >
> >> > Now I wanted to print the cause of the validation error, so I added
> the
> >> > exception message in the process method:
> >> >
> >> > String exceptionMessage = exchange.getException().getMessage();
> >> > System.out.println("Weather station '" + station + "' does not meet
> >> > validation constraints(" + exceptionMessage + "). Skipping.");
> >> >
> >> > and suddenly hell broke loose
> >> >
> >> > [2012/03/28 08:37:48.648] ERROR
> [o.a.c.p.FatalFallbackErrorHandler:done]:
> >> > Exception occurred while trying to handle previously thrown exception
> on
> >> > exchangeId: ID-BOBB-54461-1332916666871-0-3 using:
> >> >
> >>
> [Channel[Wrap[com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7
> >> ]
> >> > ->
> >> >
> >>
> com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7
> >> ]].
> >> > The previous and the new exception will be logged in the following.
> >> > [2012/03/28 08:37:48.653] ERROR
> [o.a.c.p.FatalFallbackErrorHandler:done]:
> >> > \--> Previous exception on exchangeId: ID-BOBB-54461-1332916666871-0-3
> >> > org.apache.camel.component.bean.validator.BeanValidationException:
> >> > Validation failed for:
> >> > com.mycompany.model.entities.weather.WeatherCurrent@1a4c5b4 errors:
> >> > [property: conditions; value: jasna; constraint: must match
> >> "jasno|pretežno
> >> > jasno|rahlo oblačno|delno oblačno|zmerno oblačno|pretežno
> >> > oblačno|oblačno|megla"; ]. Exchange[null] at
> >> >
> >>
> org.apache.camel.component.bean.validator.BeanValidator.process(BeanValidator.java:54)
> >> > ~[camel-bean-validator-2.9.1.jar:2.9.1] at
> >> >
> >>
> org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:101)
> >> > ~[camel-core-2.9.1.jar:2.9.1]
> >> > ...
> >> > FAILED: testNumberOfWeatherStations
> >> > java.lang.AssertionError: mock://meteo Received message count.
> Expected:
> >> > <6> but was: <12>
> >> > ...
> >> >
> >> >
> >> > What gives?
> >> >
> >> > -borut
> >>
> >>
> >>
> >> --
> >> Claus Ibsen
> >> -----------------
> >> CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
> >> FuseSource
> >> Email: cibsen@fusesource.com
> >> Web: http://fusesource.com
> >> Twitter: davsclaus, fusenews
> >> Blog: http://davsclaus.blogspot.com/
> >> Author of Camel in Action: http://www.manning.com/ibsen/
> >>
>
>
>
> --
> Claus Ibsen
> -----------------
> CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>

Re: Printing out bean validation exception message causes, well, exception

Posted by Claus Ibsen <cl...@gmail.com>.
See the simple language.
http://camel.apache.org/simple

Or try some of the scripting languages like groovy. But simple ought
to do what you want to do.


On Wed, Mar 28, 2012 at 12:05 PM, Borut Bolčina <bo...@gmail.com> wrote:
> Another thing,
>
> is there a way to get hold of current exchange in the onException chain?
>
> I would like to replace the .proccess with .log
>
> onException(BeanValidationException.class)
>  .handled(true)
> .process(new Processor() {
> public void process(Exchange exchange) throws Exception {
>  String station = ((WeatherCurrent)
> exchange.getIn().getBody()).getStation();
> Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
> Exception.class);
>  System.out.println("Weather station '" + station + "' does not meet
> validation constraints(" + cause.getMessage() + "). Skipping.");
>  }
> });
>
> So instead of using the inline Processor, something like:
>
> onException(BeanValidationException.class)
> .handled(true)
> .log("Weather station '" + ((WeatherCurrent)
> exchange.getIn().getBody()).getStation() + "' does not meet validation
> constraints(" + cause.getMessage() + "). Skipping.");
>  I know I can create a bean and pass the Exchange as parameter and do the
> logging there, but I am seeking even leaner solution.
>
> -borut
>
>
> Dne 28. marec 2012 09:05 je Claus Ibsen <cl...@gmail.com> napisal/-a:
>
>> Hi
>>
>> You handle the exception, so the getException will return null, and
>> therefore you cause a 2nd exception to occur.
>>
>> The caused exception is stored as a property on the exchange.
>> Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
>> Exception.class);
>>
>>
>>
>> On Wed, Mar 28, 2012 at 8:44 AM, Borut Bolčina <bo...@gmail.com>
>> wrote:
>> > Hello,
>> >
>> > I am using bean-validator component to validate the content of some
>> fields:
>> >
>> > @Override
>> > protected RouteBuilder createRouteBuilder() throws Exception {
>> >  return new RouteBuilder() {
>> > @Override
>> > public void configure() throws Exception {
>> >  DataFormat jaxbDataFormat = new
>> > JaxbDataFormat("com.mycompany.model.entities.weather");
>> >  onException(BeanValidationException.class)
>> > .handled(true)
>> > .process(new Processor() {
>> >  public void process(Exchange exchange) throws Exception {
>> > String station = ((WeatherCurrent)
>> exchange.getIn().getBody()).getStation();
>> >  System.out.println("Weather station '" + station + "' does not meet
>> > validation constraints. Skipping.");
>> > }
>> >  });
>> >
>> >
>> from("file:src/test/resources/weather/observation?fileName=observation_si_latest.xml&noop=true")
>> >  .split()
>> > .tokenizeXML("metData")
>> > .unmarshal(jaxbDataFormat)
>> >  .to("bean-validator://x")
>> > .to("mock:meteo");
>> >  }
>> > };
>> > }
>> >
>> > Running this test route nicely prints out the weather station which do
>> not
>> > meet the validation criteria:
>> >
>> > Weather station 'Bilje pri Novi Gorici' does not meet validation
>> > constraints. Skipping.
>> > Weather station 'Lisca' does not meet validation constraints. Skipping.
>> > Weather station 'Novo mesto' does not meet validation constraints.
>> Skipping.
>> >
>> > Now I wanted to print the cause of the validation error, so I added the
>> > exception message in the process method:
>> >
>> > String exceptionMessage = exchange.getException().getMessage();
>> > System.out.println("Weather station '" + station + "' does not meet
>> > validation constraints(" + exceptionMessage + "). Skipping.");
>> >
>> > and suddenly hell broke loose
>> >
>> > [2012/03/28 08:37:48.648] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
>> > Exception occurred while trying to handle previously thrown exception on
>> > exchangeId: ID-BOBB-54461-1332916666871-0-3 using:
>> >
>> [Channel[Wrap[com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7
>> ]
>> > ->
>> >
>> com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7
>> ]].
>> > The previous and the new exception will be logged in the following.
>> > [2012/03/28 08:37:48.653] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
>> > \--> Previous exception on exchangeId: ID-BOBB-54461-1332916666871-0-3
>> > org.apache.camel.component.bean.validator.BeanValidationException:
>> > Validation failed for:
>> > com.mycompany.model.entities.weather.WeatherCurrent@1a4c5b4 errors:
>> > [property: conditions; value: jasna; constraint: must match
>> "jasno|pretežno
>> > jasno|rahlo oblačno|delno oblačno|zmerno oblačno|pretežno
>> > oblačno|oblačno|megla"; ]. Exchange[null] at
>> >
>> org.apache.camel.component.bean.validator.BeanValidator.process(BeanValidator.java:54)
>> > ~[camel-bean-validator-2.9.1.jar:2.9.1] at
>> >
>> org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:101)
>> > ~[camel-core-2.9.1.jar:2.9.1]
>> > ...
>> > FAILED: testNumberOfWeatherStations
>> > java.lang.AssertionError: mock://meteo Received message count. Expected:
>> > <6> but was: <12>
>> > ...
>> >
>> >
>> > What gives?
>> >
>> > -borut
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
>> FuseSource
>> Email: cibsen@fusesource.com
>> Web: http://fusesource.com
>> Twitter: davsclaus, fusenews
>> Blog: http://davsclaus.blogspot.com/
>> Author of Camel in Action: http://www.manning.com/ibsen/
>>



-- 
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Printing out bean validation exception message causes, well, exception

Posted by Borut Bolčina <bo...@gmail.com>.
Another thing,

is there a way to get hold of current exchange in the onException chain?

I would like to replace the .proccess with .log

onException(BeanValidationException.class)
 .handled(true)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
 String station = ((WeatherCurrent)
exchange.getIn().getBody()).getStation();
Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
Exception.class);
 System.out.println("Weather station '" + station + "' does not meet
validation constraints(" + cause.getMessage() + "). Skipping.");
 }
});

So instead of using the inline Processor, something like:

onException(BeanValidationException.class)
.handled(true)
.log("Weather station '" + ((WeatherCurrent)
exchange.getIn().getBody()).getStation() + "' does not meet validation
constraints(" + cause.getMessage() + "). Skipping.");
 I know I can create a bean and pass the Exchange as parameter and do the
logging there, but I am seeking even leaner solution.

-borut


Dne 28. marec 2012 09:05 je Claus Ibsen <cl...@gmail.com> napisal/-a:

> Hi
>
> You handle the exception, so the getException will return null, and
> therefore you cause a 2nd exception to occur.
>
> The caused exception is stored as a property on the exchange.
> Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
> Exception.class);
>
>
>
> On Wed, Mar 28, 2012 at 8:44 AM, Borut Bolčina <bo...@gmail.com>
> wrote:
> > Hello,
> >
> > I am using bean-validator component to validate the content of some
> fields:
> >
> > @Override
> > protected RouteBuilder createRouteBuilder() throws Exception {
> >  return new RouteBuilder() {
> > @Override
> > public void configure() throws Exception {
> >  DataFormat jaxbDataFormat = new
> > JaxbDataFormat("com.mycompany.model.entities.weather");
> >  onException(BeanValidationException.class)
> > .handled(true)
> > .process(new Processor() {
> >  public void process(Exchange exchange) throws Exception {
> > String station = ((WeatherCurrent)
> exchange.getIn().getBody()).getStation();
> >  System.out.println("Weather station '" + station + "' does not meet
> > validation constraints. Skipping.");
> > }
> >  });
> >
> >
> from("file:src/test/resources/weather/observation?fileName=observation_si_latest.xml&noop=true")
> >  .split()
> > .tokenizeXML("metData")
> > .unmarshal(jaxbDataFormat)
> >  .to("bean-validator://x")
> > .to("mock:meteo");
> >  }
> > };
> > }
> >
> > Running this test route nicely prints out the weather station which do
> not
> > meet the validation criteria:
> >
> > Weather station 'Bilje pri Novi Gorici' does not meet validation
> > constraints. Skipping.
> > Weather station 'Lisca' does not meet validation constraints. Skipping.
> > Weather station 'Novo mesto' does not meet validation constraints.
> Skipping.
> >
> > Now I wanted to print the cause of the validation error, so I added the
> > exception message in the process method:
> >
> > String exceptionMessage = exchange.getException().getMessage();
> > System.out.println("Weather station '" + station + "' does not meet
> > validation constraints(" + exceptionMessage + "). Skipping.");
> >
> > and suddenly hell broke loose
> >
> > [2012/03/28 08:37:48.648] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
> > Exception occurred while trying to handle previously thrown exception on
> > exchangeId: ID-BOBB-54461-1332916666871-0-3 using:
> >
> [Channel[Wrap[com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7
> ]
> > ->
> >
> com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7
> ]].
> > The previous and the new exception will be logged in the following.
> > [2012/03/28 08:37:48.653] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
> > \--> Previous exception on exchangeId: ID-BOBB-54461-1332916666871-0-3
> > org.apache.camel.component.bean.validator.BeanValidationException:
> > Validation failed for:
> > com.mycompany.model.entities.weather.WeatherCurrent@1a4c5b4 errors:
> > [property: conditions; value: jasna; constraint: must match
> "jasno|pretežno
> > jasno|rahlo oblačno|delno oblačno|zmerno oblačno|pretežno
> > oblačno|oblačno|megla"; ]. Exchange[null] at
> >
> org.apache.camel.component.bean.validator.BeanValidator.process(BeanValidator.java:54)
> > ~[camel-bean-validator-2.9.1.jar:2.9.1] at
> >
> org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:101)
> > ~[camel-core-2.9.1.jar:2.9.1]
> > ...
> > FAILED: testNumberOfWeatherStations
> > java.lang.AssertionError: mock://meteo Received message count. Expected:
> > <6> but was: <12>
> > ...
> >
> >
> > What gives?
> >
> > -borut
>
>
>
> --
> Claus Ibsen
> -----------------
> CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>

Re: Printing out bean validation exception message causes, well, exception

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You handle the exception, so the getException will return null, and
therefore you cause a 2nd exception to occur.

The caused exception is stored as a property on the exchange.
Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
Exception.class);



On Wed, Mar 28, 2012 at 8:44 AM, Borut Bolčina <bo...@gmail.com> wrote:
> Hello,
>
> I am using bean-validator component to validate the content of some fields:
>
> @Override
> protected RouteBuilder createRouteBuilder() throws Exception {
>  return new RouteBuilder() {
> @Override
> public void configure() throws Exception {
>  DataFormat jaxbDataFormat = new
> JaxbDataFormat("com.mycompany.model.entities.weather");
>  onException(BeanValidationException.class)
> .handled(true)
> .process(new Processor() {
>  public void process(Exchange exchange) throws Exception {
> String station = ((WeatherCurrent) exchange.getIn().getBody()).getStation();
>  System.out.println("Weather station '" + station + "' does not meet
> validation constraints. Skipping.");
> }
>  });
>
> from("file:src/test/resources/weather/observation?fileName=observation_si_latest.xml&noop=true")
>  .split()
> .tokenizeXML("metData")
> .unmarshal(jaxbDataFormat)
>  .to("bean-validator://x")
> .to("mock:meteo");
>  }
> };
> }
>
> Running this test route nicely prints out the weather station which do not
> meet the validation criteria:
>
> Weather station 'Bilje pri Novi Gorici' does not meet validation
> constraints. Skipping.
> Weather station 'Lisca' does not meet validation constraints. Skipping.
> Weather station 'Novo mesto' does not meet validation constraints. Skipping.
>
> Now I wanted to print the cause of the validation error, so I added the
> exception message in the process method:
>
> String exceptionMessage = exchange.getException().getMessage();
> System.out.println("Weather station '" + station + "' does not meet
> validation constraints(" + exceptionMessage + "). Skipping.");
>
> and suddenly hell broke loose
>
> [2012/03/28 08:37:48.648] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
> Exception occurred while trying to handle previously thrown exception on
> exchangeId: ID-BOBB-54461-1332916666871-0-3 using:
> [Channel[Wrap[com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7]
> ->
> com.mycompany.datarobot.weather.WeatherCurrentValidateConditionNotEmptyTest$1$1@15e92d7]].
> The previous and the new exception will be logged in the following.
> [2012/03/28 08:37:48.653] ERROR [o.a.c.p.FatalFallbackErrorHandler:done]:
> \--> Previous exception on exchangeId: ID-BOBB-54461-1332916666871-0-3
> org.apache.camel.component.bean.validator.BeanValidationException:
> Validation failed for:
> com.mycompany.model.entities.weather.WeatherCurrent@1a4c5b4 errors:
> [property: conditions; value: jasna; constraint: must match "jasno|pretežno
> jasno|rahlo oblačno|delno oblačno|zmerno oblačno|pretežno
> oblačno|oblačno|megla"; ]. Exchange[null] at
> org.apache.camel.component.bean.validator.BeanValidator.process(BeanValidator.java:54)
> ~[camel-bean-validator-2.9.1.jar:2.9.1] at
> org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:101)
> ~[camel-core-2.9.1.jar:2.9.1]
> ...
> FAILED: testNumberOfWeatherStations
> java.lang.AssertionError: mock://meteo Received message count. Expected:
> <6> but was: <12>
> ...
>
>
> What gives?
>
> -borut



-- 
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/