alokraop
alokraop

Reputation: 867

How to route error messages rippling up from an http outbound gateway on http status code?

I have a gateway with a custom error channel configured to receive error messages coming from an http outbound gateway deeper in the flow.

@MessagingGateway(errorChannel="invocationFailureChannel")
public interface Invocator {
    @Gateway(requestChannel="invocationChannel")
    public Boolean invokeService(Message<String> invocation);
}

When the outbound gateway fails to call it's designated endpoint, i end up with a MessageHandlingException on the error channel. It's got a cause which tells me what went wrong, but i want to route the message based on the status code rather than the exception object i'm using now to sort of be a substitute for the status (all exceptions subclassing IOException are non 500):

public ErrorType determineErrorType(Throwable cause) {
    return (cause.getCause() instanceof  IOException ? 
                ErrorType.DOWNSTREAM_UNAVAILABILITY : 
                ErrorType.INVALID_DATA);
}

It would be much cleaner if i actually routed on the status code though, coz in the future there might be other flows for other status codes. Plus i'm not even sure the condition i have in place covers all possible exceptions.

Thanks for the help.

EDIT:

Hey i tried to send a request to a non existent url and inspect the exception payload, but the cause is a ResourceAccessException/UnknownHostException rather than an HttpStatusCodeException.

Exception payload

Now there's a lot of intermediate components between the gateway and the eventual http outbound gateway, but that won't matter right? Here's the actual gateway configuration:

.handle(Http.outboundGateway(spelParser.parseExpression("headers." + HeaderKeys.TARGET_ENDPOINT))
            .extractPayload(true)
            .httpMethod(HttpMethod.POST)
            .expectedResponseType(String.class)
            .requestFactory(requestFactory())
            .get()
        , httpOutboundEndpointSpec())
.wireTap("outboundLoggerChannel")
.handle(new GenericHandler<String>() {
    @Override
    public Object handle(String reply, Map<String, Object> headers) {
        System.out.println("Logging response");
        return true;
    }
})

When the outbound gateway returns any status other than 200, the exception ignores the wiretap after the gateway call and directly jumps up to the error channel configured on the @Gateway right? I don't really know what i'm doing wrong.

Upvotes: 0

Views: 178

Answers (1)

Gary Russell
Gary Russell

Reputation: 174769

You need to show your error flow but, the MessagingException.cause() is most likely a HttpStatusCodeException (client - 4xx or server - 5xx) with a statusCode property.

Upvotes: 1

Related Questions