You are right, JPA has eaten the original exception. Bad guy.

Regards,

Charles Moulliard
Senior Enterprise Architect
Apache Camel Committer

*****************************
blog : http://cmoulliard.blogspot.com
twitter : http://twitter.com/cmoulliard
Linkedlin : http://www.linkedin.com/in/charlesmoulliard

Apache Camel Group :
http://www.linkedin.com/groups?home=&gid=2447439&trk=anet_ug_hm


On Thu, Dec 10, 2009 at 10:11 AM, Willem Jiang <[email protected]>wrote:

> Hi Charles,
>
> I think this is the JPAEndpoint eat up the original exception.
> The InvalidPayloadException has the original exception information, but
> the new create the InvalidPayloadRuntimeException doesn't reflect.
>
> I will did a quick fix for that.
>
>  protected Expression createProducerExpression() {
>        return new ExpressionAdapter() {
>            public Object evaluate(Exchange exchange) {
>                Object answer;
>
>                // must have a body
>                try {
>                    if (getEntityType() == null) {
>                        answer = exchange.getIn().getMandatoryBody();
>                    } else {
>                        answer =
> exchange.getIn().getMandatoryBody(getEntityType());
>                    }
>                } catch (InvalidPayloadException e) {
>                    throw new InvalidPayloadRuntimeException(exchange,
> getEntityType());
>                }
>
>                if (answer == null) {
>                    throw new InvalidPayloadRuntimeException(exchange,
> getEntityType());
>                } else {
>                    return answer;
>                }
>            }
>        };
>    }
>
>
> Willem
>
>
>
> Charles Moulliard wrote:
>
>> If we look to the following code
>>
>>
>> @Converter
>> public class CustomerTransformer {
>>
>>    private static final transient Log LOG =
>> LogFactory.getLog(CustomerTransformer.class);
>>
>>    /**
>>     * A transformation method to convert a person document into a customer
>>     * entity
>>     * @throws Exception
>>     */
>>    @Converter
>>    public CustomerEntity toCustomer(PersonDocument doc, Exchange exchange)
>> throws Exception {
>>        JpaTemplate template =
>> exchange.getIn().getHeader("CamelJpaTemplate", JpaTemplate.class);
>>
>>
>>        String user = doc.getUser();
>>        CustomerEntity customer = findCustomerByName(template, user);
>>
>>        // let's convert information from the document into the entity bean
>>        customer.setUserName(user);
>>        customer.setFirstName(doc.getFirstName());
>>        customer.setSurname(doc.getLastName());
>>        customer.setCity(doc.getCity());
>>
>>        LOG.debug("Created object customer: " + customer);
>>        return customer;
>>    }
>>
>>    /**
>>     * Finds a customer for the given username
>>     */
>>    protected CustomerEntity findCustomerByName(JpaTemplate template,
>> String
>> user) throws Exception {
>>        List<CustomerEntity> list = CastUtils.cast(template.find("select x
>> from customer"
>>                                                                 + " x
>> where
>> x.userName = ?1", user));
>>        if (list.isEmpty()) {
>>            CustomerEntity answer = new CustomerEntity();
>>            answer.setUserName(user);
>>            return answer;
>>        } else {
>>            return list.get(0);
>>        }
>>    }
>>
>> }
>>
>>
>>
>> any error which is raised during call to the method findCustomerByNamewill
>> not be propagated to the camel route and by consequence, the user see a
>> false message on the console :
>>
>> 09:17:10,869 | INFO  | 3: FileComponent | Tracer
>> |
>> rg.apache.camel.processor.Logger   88 |
>> b7394fe8-656d-4b44-aba1-591c75a296eb
>>
>>> (route3)  --> jpa://org.apache.camel.example.etl.CustomerEntity <<<
>>>>>
>>>> Pattern:InOnly, Headers:{CamelFileLength=984,
>> CamelFileNameOnly=row1.xml,
>> CamelFilePath=src\data\row1.xml, CamelFileParent=src\data,
>> CamelFileLastModified=Tue Jul 14 10:37:40 CEST 2009,
>>
>> CamelFileAbsolutePath=D:\Temp\tutorial\camel-example-etl\apache-felix-karaf-1.1.0-SNAPSHOT\src\data\row1.xml,
>> CamelFileAbsolute=false, CamelFileRelativePath=row1.xml,
>> CamelFileName=row1.xml},
>> BodyType:org.apache.camel.example.etl.PersonDocument, Body:<person
>>
>> user="james"><firstName>James</firstName><lastName>Strachan</lastName><city>London</city></person>
>> 09:17:12,119 | ERROR | 3: FileComponent | GenericFileOnCompletion
>>  |
>> rg.apache.camel.processor.Logger  248 | No body available of type:
>> org.apache.camel.example.etl.CustomerEntity but has value: Person[user:
>> james] of type: org.apache.camel.example.etl.PersonDocument on:
>> GenericFileMessage with body: Person[user: james] on the exchange:
>> Exchange[GenericFileMessage with body: Person[user: james]]
>> org.apache.camel.InvalidPayloadRuntimeException: No body available of
>> type:
>> org.apache.camel.example.etl.CustomerEntity but has value: Person[user:
>> james] of type: org.apache.camel.example.etl.PersonDocument on:
>> GenericFileMessage with body: Person[user: james] on the exchange:
>> Exchange[GenericFileMessage with body: Person[user: james]]
>>    at
>>
>> org.apache.camel.component.jpa.JpaEndpoint$1.evaluate(JpaEndpoint.java:268)
>>    at
>>
>> org.apache.camel.impl.ExpressionAdapter.evaluate(ExpressionAdapter.java:36)
>>    at
>> org.apache.camel.component.jpa.JpaProducer.process(JpaProducer.java:47)
>>    a
>>
>> Question : Should the TypeConverter be blocking for camel route or not in
>> case of error ?
>>
>> Regards,
>>
>> Charles Moulliard
>> Senior Enterprise Architect
>> Apache Camel Committer
>>
>> *****************************
>> blog : http://cmoulliard.blogspot.com
>> twitter : http://twitter.com/cmoulliard
>> Linkedlin : http://www.linkedin.com/in/charlesmoulliard
>>
>> Apache Camel Group :
>> http://www.linkedin.com/groups?home=&gid=2447439&trk=anet_ug_hm
>>
>>
>

Reply via email to