GH-8893: ExpERHA: Copy headers into ErrorMessage

Fixes: #8893

Simplify the target flows logic for `ExpressionEvaluatingRequestHandlerAdvice.failureChannel`
when no exception is re-thrown and target flow would like to produce a reply.

Currently, there is need in extra logic like `.enrichHeaders(e -> e.replyChannelExpression("payload.failedMessage.headers[replyChannel]"))`

* Copy failed `Message` headers into an `ErrorMessage` before sending it into the `failureChannel` from the `ExpressionEvaluatingRequestHandlerAdvice`
This commit is contained in:
Artem Bilan
2024-02-07 14:18:27 -05:00
parent dbd46dfe3d
commit 34bdff04c4
2 changed files with 11 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -77,8 +77,7 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
private EvaluationContext evaluationContext;
/**
* Set the expression to evaluate against the message after a successful
* handler invocation.
* Set the expression to evaluate against the message after a successful handler invocation.
* Defaults to {@code payload}, if {@code successChannel} is configured.
* @param onSuccessExpression the SpEL expression.
* @since 4.3.7
@@ -88,8 +87,7 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
}
/**
* Set the expression to evaluate against the message after a successful
* handler invocation.
* Set the expression to evaluate against the message after a successful handler invocation.
* Defaults to {@code payload}, if {@code successChannel} is configured.
* @param onSuccessExpression the SpEL expression.
* @since 5.0
@@ -228,7 +226,7 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
}
if (!this.trapException) {
if (e instanceof ThrowableHolderException) { // NOSONAR
throw (ThrowableHolderException) e;
throw e;
}
else {
throw new ThrowableHolderException(actualException); // NOSONAR lost stack trace
@@ -276,7 +274,7 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
MessagingException messagingException =
new MessageHandlingExpressionEvaluatingAdviceException(message, "Handler Failed",
unwrapThrowableIfNecessary(exception), evalResult);
ErrorMessage errorMessage = new ErrorMessage(messagingException);
ErrorMessage errorMessage = new ErrorMessage(messagingException, message.getHeaders());
this.messagingTemplate.send(this.failureChannel, errorMessage);
}
return evalResult;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.integration.handler.advice;
import java.util.Map;
import org.aopalliance.aop.Advice;
import org.junit.jupiter.api.Test;
@@ -55,13 +57,15 @@ public class ExpressionEvaluatingRequestHandlerAdviceTests {
@Test
public void test() {
this.in.send(new GenericMessage<>("good"));
this.in.send(new GenericMessage<>("junk"));
this.in.send(new GenericMessage<>("junk", Map.of("some_request_header_key", "some_request_header_value")));
assertThat(config.successful).isInstanceOf(AdviceMessage.class);
assertThat(config.successful.getPayload()).isEqualTo("good was successful");
assertThat(config.failed).isInstanceOf(ErrorMessage.class);
Object evaluationResult = ((MessageHandlingExpressionEvaluatingAdviceException) config.failed.getPayload())
.getEvaluationResult();
assertThat((String) evaluationResult).startsWith("junk was bad, with reason:");
assertThat(config.failed.getHeaders()).containsEntry("some_request_header_key", "some_request_header_value");
}
@Configuration