I have a spring-integration transformer that accepts a org.w3c.dom.Document and returns a domain object. And this is nice. If there are elements missing I raise an application exception.
However, I'd like to get that exception onto the error channel but instead the way it currently works by bubbling back through a chain of handlers. It would be nice if there a way of specifying an error channel in the case of a failed transform.
I could:
- pass the message through a router to check for missing elements before (or after) the transformer
- route the message
However that means both parsing the document twice and a bit of a re-write.
The answer I came up with was to change the return type of the transformer from the domain POJO to a Message. And then, in the exception case, to return a Message. The exception is then routed to the correct handler by a Payload Type Router.
In order to determine where exception messages should go, you need to set the "errorChannel" header. For example,
This chain will assign the error channel header before calling the transformer. If the transformer succeeds, it continues down the chain, but if it throws an exception a
MessagingException
will be sent as a message to myErrorChannel. (If you want the way to handle an exception to be different later on in the chain, you could have another header-enricher after the transformer to update the errorChannel header to the next place you want to send exceptions.)Refer to the details in the Error Handling section of the Spring Integration documentation.