Search code examples
xmlspringspring-integrationdeclarative

Within an API defined with spring-integration declarative, how to get context information for exception messages?


We are working with Spring-Integration for implementing APIs which themselves call several different APIs.

In case of an exception, we have an processor on the errorChannel that introspects the information contained within ErrorMessage which gives a great deal of detail.

I was looking for way of augmenting the ErrorMessage with information about the current processor that was executing at the time of the exception so as to provide more pertinent response information in the error processor.

I was thinking, for instance, that one could put a WireTap global interceptor which adds a "channelName" header to the current message and that "channelName" header would be available within the ErrorMessage.

But I'm wondering if there isn't something more direct than "channelName". Perhaps there is some context information available already within ErrorMessage that indicates the current processor at the time of the exception.

All pointers appreciated


Solution

  • The channel name, as well as many other components, can be tracked by the message history pattern. It adds some headers as a list to gather information about components the message is traveling through. See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/system-management.html#message-history

    At the same time the exception message must already have an info about failed components. See how it was fixed some time ago: https://github.com/spring-projects/spring-integration/pull/2986.

    For example a MessageRejectedException from the filter component may this info the exception message:

    message has been rejected in filter: bean 'gatewayRequestFlow.filter#0' for component 'gatewayRequestFlow.org.springframework.integration.config.ConsumerEndpointFactoryBean#0'; 
    defined in: 'org.springframework.integration.dsl.gateway.GatewayDslTests$ContextConfiguration'; 
    from source: 'bean method gatewayRequestFlow'
    

    This one should lead somehow to this code:

        @Bean
        public IntegrationFlow gatewayRequestFlow() {
            return IntegrationFlow.from("gatewayRequest")
                    .filter("foo"::equals, (f) -> f.throwExceptionOnRejection(true))
                    .<String, String>transform(String::toUpperCase)
                    .get();
        }
    

    If you add an explicit id for that filter, it will appear in that message instead of a bit crypticgatewayRequestFlow.filter#0 bean name.