You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/09/04 08:13:14 UTC
[camel-k-runtime] branch master updated: camel-k-native-http:
improve producer error handling
This is an automated email from the ASF dual-hosted git repository.
lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
The following commit(s) were added to refs/heads/master by this push:
new b44132f camel-k-native-http: improve producer error handling
b44132f is described below
commit b44132fa248b6f4c7fbe57fcc7c13695c6fceb4d
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Wed Sep 4 10:13:02 2019 +0200
camel-k-native-http: improve producer error handling
---
camel-knative-http/pom.xml | 5 +++
.../knative/http/KnativeHttpProducer.java | 42 ++++++++++++++--------
.../component/knative/http/KnativeHttpTest.java | 38 ++++++++++++++++++++
3 files changed, 71 insertions(+), 14 deletions(-)
diff --git a/camel-knative-http/pom.xml b/camel-knative-http/pom.xml
index bbd8d4b..0054af3 100644
--- a/camel-knative-http/pom.xml
+++ b/camel-knative-http/pom.xml
@@ -118,6 +118,11 @@
<artifactId>camel-undertow</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-bean</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
diff --git a/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducer.java b/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducer.java
index f42adf0..c902841 100644
--- a/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducer.java
+++ b/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducer.java
@@ -91,27 +91,41 @@ public class KnativeHttpProducer extends DefaultAsyncProducer {
client.post(endpoint.getPort(), endpoint.getHost(), endpoint.getPath())
.putHeaders(headers)
.sendBuffer(Buffer.buffer(payload), response -> {
- HttpResponse<Buffer> result = response.result();
+ if (response.succeeded()) {
+ HttpResponse<Buffer> result = response.result();
- Message answer = new DefaultMessage(exchange.getContext());
- answer.setHeader(Exchange.HTTP_RESPONSE_CODE, result.statusCode());
+ Message answer = new DefaultMessage(exchange.getContext());
+ answer.setHeader(Exchange.HTTP_RESPONSE_CODE, result.statusCode());
- for (Map.Entry<String, String> entry : result.headers().entries()) {
- if (!endpoint.getHeaderFilterStrategy().applyFilterToExternalHeaders(entry.getKey(), entry.getValue(), exchange)) {
- KnativeHttpSupport.appendHeader(message.getHeaders(), entry.getKey(), entry.getValue());
+ for (Map.Entry<String, String> entry : result.headers().entries()) {
+ if (!endpoint.getHeaderFilterStrategy().applyFilterToExternalHeaders(entry.getKey(), entry.getValue(), exchange)) {
+ KnativeHttpSupport.appendHeader(answer.getHeaders(), entry.getKey(), entry.getValue());
+ }
}
- }
-
- exchange.setMessage(answer);
- if (response.succeeded()) {
answer.setBody(result.body().getBytes());
+
+ if (result.statusCode() < 200 || result.statusCode() >= 300) {
+ String exceptionMessage = String.format(
+ "HTTP operation failed invoking %s with statusCode: %d, statusMessage: %s",
+ URISupport.sanitizeUri(getURI()),
+ result.statusCode(),
+ result.statusMessage()
+ );
+
+ exchange.setException(new CamelException(exceptionMessage));
+ }
+
+ answer.setHeader(Exchange.HTTP_RESPONSE_CODE, result.statusCode());
+
+ exchange.setMessage(answer);
} else if (response.failed() && endpoint.getThrowExceptionOnFailure()) {
- Exception cause = new CamelException(
- "HTTP operation failed invoking " + URISupport.sanitizeUri(getURI()) + " with statusCode: " + result.statusCode()
- );
+ String exceptionMessage = "HTTP operation failed invoking " + URISupport.sanitizeUri(getURI());
+ if (response.result() != null) {
+ exceptionMessage += " with statusCode: " + response.result().statusCode();
+ }
- exchange.setException(cause);
+ exchange.setException(new CamelException(exceptionMessage));
}
callback.done(false);
diff --git a/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java b/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java
index 42fae3d..736f9ff 100644
--- a/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java
+++ b/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java
@@ -17,6 +17,8 @@
package org.apache.camel.component.knative.http;
import org.apache.camel.CamelContext;
+import org.apache.camel.CamelException;
+import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
@@ -229,5 +231,41 @@ public class KnativeHttpTest {
mock1.assertIsSatisfied();
mock2.assertIsSatisfied();
}
+
+ @Test
+ void testInvokeNotExistingEndpoint() throws Exception {
+ RouteBuilder.addRoutes(context, b -> {
+ b.from("direct:start")
+ .toF("knative-http:0.0.0.0:%d", port)
+ .to("mock:start");
+ });
+
+ context.start();
+
+ Exchange exchange = template.request("direct:start", e -> e.getMessage().setBody(""));
+ assertThat(exchange.isFailed()).isTrue();
+ assertThat(exchange.getException()).isInstanceOf(CamelException.class);
+ assertThat(exchange.getException()).hasMessageStartingWith("HTTP operation failed invoking");
+ }
+
+ @Test
+ void testInvokeEndpointWithError() throws Exception {
+ RouteBuilder.addRoutes(context, b -> {
+ b.from("direct:start")
+ .toF("knative-http:0.0.0.0:%d", port)
+ .to("mock:start");
+ b.fromF("undertow:http://0.0.0.0:%d", port)
+ .routeId("endpoint")
+ .process(e -> { throw new RuntimeException("endpoint error"); });
+ });
+
+ context.start();
+
+ Exchange exchange = template.request("direct:start", e -> e.getMessage().setBody(""));
+ assertThat(exchange.isFailed()).isTrue();
+ assertThat(exchange.getException()).isInstanceOf(CamelException.class);
+ assertThat(exchange.getException()).hasMessageStartingWith("HTTP operation failed invoking");
+ assertThat(exchange.getException()).hasMessageContaining("with statusCode: 500, statusMessage: Internal Server Error");
+ }
}