This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 08b7ddd86dee CAMEL-27755: camel-alibaba-fc - set the 
CamelAlibabaFcFunctionError header for a function error (#31079)
08b7ddd86dee is described below

commit 08b7ddd86dee2a4249a456a28f366f1445d63116
Author: Saravanakumar Selvaraj <[email protected]>
AuthorDate: Mon Nov 30 08:33:31 2026 +0000

    CAMEL-27755: camel-alibaba-fc - set the CamelAlibabaFcFunctionError header 
for a function error (#31079)
    
    Function Compute can answer InvokeFunction for a function error with the 
status 200 and the response header
    X-Fc-Error-Type. tea-openapi throws only for a status from 400 to 599, so 
the exchange completed. No Camel header
    showed the error. It was only in the headers Map of the body, as the key 
x-fc-error-type.
    
    invokeFunction now sets the new header CamelAlibabaFcFunctionError to the 
value of x-fc-error-type, such as
    UnhandledInvocationError. For a response without an error type, it removes 
the header of an earlier call. The exchange
    still completes, as camel-aws2-lambda does for CamelAwsLambdaFunctionError. 
The doc page says how a route detects such
    a function error.
    
    The body, the producerOut text and the other headers stay as they are. The 
component JSON, its catalog copy, the
    catalog copy of the doc page and the Endpoint DSL are regenerated. The new 
InvokeFunctionErrorTest covers a function
    error and two calls in one route.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/catalog/components/alibaba-fc.json       |   3 +-
 .../camel/catalog/docs/alibaba-fc-component.adoc   |   6 ++
 .../camel/component/alibaba/fc/alibaba-fc.json     |   3 +-
 .../src/main/docs/alibaba-fc-component.adoc        |   6 ++
 .../camel/component/alibaba/fc/FCProducer.java     |   6 ++
 .../component/alibaba/fc/constants/FCHeaders.java  |   8 ++
 .../alibaba/fc/InvokeFunctionErrorTest.java        | 109 +++++++++++++++++++++
 .../endpoint/dsl/FCEndpointBuilderFactory.java     |  16 +++
 8 files changed, 155 insertions(+), 2 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/alibaba-fc.json
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/alibaba-fc.json
index ec7aacd82b0c..3d2402aa9977 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/alibaba-fc.json
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/alibaba-fc.json
@@ -35,7 +35,8 @@
     "CamelAlibabaFcFunctionName": { "index": 2, "kind": "header", 
"displayName": "", "group": "producer", "label": "producer", "required": false, 
"javaType": "String", "deprecated": false, "autowired": false, "secret": false, 
"description": "The name of the function. This header replaces the functionName 
option. If this header is not set, an exchange property of the same name 
replaces the functionName option.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders#FU [...]
     "CamelAlibabaFcQualifier": { "index": 3, "kind": "header", "displayName": 
"", "group": "producer", "label": "producer", "required": false, "javaType": 
"String", "deprecated": false, "autowired": false, "secret": false, 
"description": "The version or alias of the service. This header replaces the 
qualifier option. If this header is not set, an exchange property of the same 
name replaces the qualifier option.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders# [...]
     "CamelAlibabaFcRequestId": { "index": 4, "kind": "header", "displayName": 
"", "group": "producer", "label": "producer", "required": false, "javaType": 
"String", "deprecated": false, "autowired": false, "secret": false, 
"description": "The request ID that Function Compute returns in the response to 
invokeFunction. The producer sets it for invokeFunction, unless the exchange 
fails.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders#REQUEST_ID" },
-    "CamelAlibabaFcStatusCode": { "index": 5, "kind": "header", "displayName": 
"", "group": "producer", "label": "producer", "required": false, "javaType": 
"Integer", "deprecated": false, "autowired": false, "secret": false, 
"description": "The HTTP status code of the response to the operation. The 
producer sets it, unless the exchange fails.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders#STATUS_CODE" }
+    "CamelAlibabaFcStatusCode": { "index": 5, "kind": "header", "displayName": 
"", "group": "producer", "label": "producer", "required": false, "javaType": 
"Integer", "deprecated": false, "autowired": false, "secret": false, 
"description": "The HTTP status code of the response to the operation. The 
producer sets it, unless the exchange fails.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders#STATUS_CODE" },
+    "CamelAlibabaFcFunctionError": { "index": 6, "kind": "header", 
"displayName": "", "group": "producer", "label": "producer", "required": false, 
"javaType": "String", "deprecated": false, "autowired": false, "secret": false, 
"description": "The error type that Function Compute returns with the status 
200 for a function error, such as UnhandledInvocationError. The producer sets 
it for invokeFunction, or removes it if the response has none, unless the 
exchange fails. Such a function erro [...]
   },
   "bodyTypes": {
     "producerIn": { "carriers": [ "any" ], "byOption": [ { "option": 
"operation", "value": "invokeFunction", "carriers": [ "text", "bytes", 
"stream", "file", "null" ] } ], "description": "invokeFunction sends the body 
as the payload of the function: text in UTF-8, bytes as they are, a stream or a 
file as its bytes, so binary data is kept, and no body as an empty payload. A 
Map or a POJO fails, as it does not convert to bytes, so marshal it first. 
getFunction does not use the body" },
diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-fc-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-fc-component.adoc
index c156d94673bb..60aed42ffab8 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-fc-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-fc-component.adoc
@@ -58,6 +58,12 @@ file is sent as its bytes, so binary data is kept. Without a 
body the function i
 or a POJO does not convert to bytes and fails with an 
`InvalidPayloadException`, so marshal it first, for example to
 JSON with `marshal().json()`.
 
+=== Function error
+
+A function error that Function Compute returns with the status 200 does not 
fail the exchange. The producer sets the
+`CamelAlibabaFcFunctionError` header to its error type, such as 
`UnhandledInvocationError`. A route detects such a
+function error by this header, for example in a choice.
+
 === Endpoint of Function Compute
 
 The `endpoint` option is the endpoint of Function Compute for your Alibaba 
Cloud account and region. The public
diff --git 
a/components/camel-alibaba/camel-alibaba-fc/src/generated/resources/META-INF/org/apache/camel/component/alibaba/fc/alibaba-fc.json
 
b/components/camel-alibaba/camel-alibaba-fc/src/generated/resources/META-INF/org/apache/camel/component/alibaba/fc/alibaba-fc.json
index ec7aacd82b0c..3d2402aa9977 100644
--- 
a/components/camel-alibaba/camel-alibaba-fc/src/generated/resources/META-INF/org/apache/camel/component/alibaba/fc/alibaba-fc.json
+++ 
b/components/camel-alibaba/camel-alibaba-fc/src/generated/resources/META-INF/org/apache/camel/component/alibaba/fc/alibaba-fc.json
@@ -35,7 +35,8 @@
     "CamelAlibabaFcFunctionName": { "index": 2, "kind": "header", 
"displayName": "", "group": "producer", "label": "producer", "required": false, 
"javaType": "String", "deprecated": false, "autowired": false, "secret": false, 
"description": "The name of the function. This header replaces the functionName 
option. If this header is not set, an exchange property of the same name 
replaces the functionName option.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders#FU [...]
     "CamelAlibabaFcQualifier": { "index": 3, "kind": "header", "displayName": 
"", "group": "producer", "label": "producer", "required": false, "javaType": 
"String", "deprecated": false, "autowired": false, "secret": false, 
"description": "The version or alias of the service. This header replaces the 
qualifier option. If this header is not set, an exchange property of the same 
name replaces the qualifier option.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders# [...]
     "CamelAlibabaFcRequestId": { "index": 4, "kind": "header", "displayName": 
"", "group": "producer", "label": "producer", "required": false, "javaType": 
"String", "deprecated": false, "autowired": false, "secret": false, 
"description": "The request ID that Function Compute returns in the response to 
invokeFunction. The producer sets it for invokeFunction, unless the exchange 
fails.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders#REQUEST_ID" },
-    "CamelAlibabaFcStatusCode": { "index": 5, "kind": "header", "displayName": 
"", "group": "producer", "label": "producer", "required": false, "javaType": 
"Integer", "deprecated": false, "autowired": false, "secret": false, 
"description": "The HTTP status code of the response to the operation. The 
producer sets it, unless the exchange fails.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders#STATUS_CODE" }
+    "CamelAlibabaFcStatusCode": { "index": 5, "kind": "header", "displayName": 
"", "group": "producer", "label": "producer", "required": false, "javaType": 
"Integer", "deprecated": false, "autowired": false, "secret": false, 
"description": "The HTTP status code of the response to the operation. The 
producer sets it, unless the exchange fails.", "constantName": 
"org.apache.camel.component.alibaba.fc.constants.FCHeaders#STATUS_CODE" },
+    "CamelAlibabaFcFunctionError": { "index": 6, "kind": "header", 
"displayName": "", "group": "producer", "label": "producer", "required": false, 
"javaType": "String", "deprecated": false, "autowired": false, "secret": false, 
"description": "The error type that Function Compute returns with the status 
200 for a function error, such as UnhandledInvocationError. The producer sets 
it for invokeFunction, or removes it if the response has none, unless the 
exchange fails. Such a function erro [...]
   },
   "bodyTypes": {
     "producerIn": { "carriers": [ "any" ], "byOption": [ { "option": 
"operation", "value": "invokeFunction", "carriers": [ "text", "bytes", 
"stream", "file", "null" ] } ], "description": "invokeFunction sends the body 
as the payload of the function: text in UTF-8, bytes as they are, a stream or a 
file as its bytes, so binary data is kept, and no body as an empty payload. A 
Map or a POJO fails, as it does not convert to bytes, so marshal it first. 
getFunction does not use the body" },
diff --git 
a/components/camel-alibaba/camel-alibaba-fc/src/main/docs/alibaba-fc-component.adoc
 
b/components/camel-alibaba/camel-alibaba-fc/src/main/docs/alibaba-fc-component.adoc
index c156d94673bb..60aed42ffab8 100644
--- 
a/components/camel-alibaba/camel-alibaba-fc/src/main/docs/alibaba-fc-component.adoc
+++ 
b/components/camel-alibaba/camel-alibaba-fc/src/main/docs/alibaba-fc-component.adoc
@@ -58,6 +58,12 @@ file is sent as its bytes, so binary data is kept. Without a 
body the function i
 or a POJO does not convert to bytes and fails with an 
`InvalidPayloadException`, so marshal it first, for example to
 JSON with `marshal().json()`.
 
+=== Function error
+
+A function error that Function Compute returns with the status 200 does not 
fail the exchange. The producer sets the
+`CamelAlibabaFcFunctionError` header to its error type, such as 
`UnhandledInvocationError`. A route detects such a
+function error by this header, for example in a choice.
+
 === Endpoint of Function Compute
 
 The `endpoint` option is the endpoint of Function Compute for your Alibaba 
Cloud account and region. The public
diff --git 
a/components/camel-alibaba/camel-alibaba-fc/src/main/java/org/apache/camel/component/alibaba/fc/FCProducer.java
 
b/components/camel-alibaba/camel-alibaba-fc/src/main/java/org/apache/camel/component/alibaba/fc/FCProducer.java
index 91c14cd8a8f1..07bc240ecd87 100644
--- 
a/components/camel-alibaba/camel-alibaba-fc/src/main/java/org/apache/camel/component/alibaba/fc/FCProducer.java
+++ 
b/components/camel-alibaba/camel-alibaba-fc/src/main/java/org/apache/camel/component/alibaba/fc/FCProducer.java
@@ -73,6 +73,12 @@ public class FCProducer extends DefaultProducer {
         if (response.getHeaders() != null && 
ObjectHelper.isNotEmpty(response.getHeaders().get("x-fc-request-id"))) {
             exchange.getMessage().setHeader(FCHeaders.REQUEST_ID, 
response.getHeaders().get("x-fc-request-id"));
         }
+        // a response without an error type removes the header of an earlier 
call
+        if (response.getHeaders() != null && 
ObjectHelper.isNotEmpty(response.getHeaders().get("x-fc-error-type"))) {
+            exchange.getMessage().setHeader(FCHeaders.FUNCTION_ERROR, 
response.getHeaders().get("x-fc-error-type"));
+        } else {
+            exchange.getMessage().removeHeader(FCHeaders.FUNCTION_ERROR);
+        }
     }
 
     private void getFunction(Exchange exchange, ClientConfigurations 
configuration, Client fcClient) throws Exception {
diff --git 
a/components/camel-alibaba/camel-alibaba-fc/src/main/java/org/apache/camel/component/alibaba/fc/constants/FCHeaders.java
 
b/components/camel-alibaba/camel-alibaba-fc/src/main/java/org/apache/camel/component/alibaba/fc/constants/FCHeaders.java
index fd64db2c470b..84f25f5f74c6 100644
--- 
a/components/camel-alibaba/camel-alibaba-fc/src/main/java/org/apache/camel/component/alibaba/fc/constants/FCHeaders.java
+++ 
b/components/camel-alibaba/camel-alibaba-fc/src/main/java/org/apache/camel/component/alibaba/fc/constants/FCHeaders.java
@@ -57,6 +57,14 @@ public final class FCHeaders {
               javaType = "Integer")
     public static final String STATUS_CODE = "CamelAlibabaFcStatusCode";
 
+    @Metadata(label = "producer",
+              description = "The error type that Function Compute returns with 
the status 200 for a function error, such as"
+                            + " UnhandledInvocationError. The producer sets it 
for invokeFunction, or removes it if the"
+                            + " response has none, unless the exchange fails. 
Such a function error does not fail the"
+                            + " exchange, so a route detects it by this 
header.",
+              javaType = "String")
+    public static final String FUNCTION_ERROR = "CamelAlibabaFcFunctionError";
+
     private FCHeaders() {
     }
 }
diff --git 
a/components/camel-alibaba/camel-alibaba-fc/src/test/java/org/apache/camel/component/alibaba/fc/InvokeFunctionErrorTest.java
 
b/components/camel-alibaba/camel-alibaba-fc/src/test/java/org/apache/camel/component/alibaba/fc/InvokeFunctionErrorTest.java
new file mode 100644
index 000000000000..0af6749594ed
--- /dev/null
+++ 
b/components/camel-alibaba/camel-alibaba-fc/src/test/java/org/apache/camel/component/alibaba/fc/InvokeFunctionErrorTest.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.alibaba.fc;
+
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.aliyun.fc_open20210406.Client;
+import com.aliyun.fc_open20210406.models.InvokeFunctionRequest;
+import com.aliyun.fc_open20210406.models.InvokeFunctionResponse;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.alibaba.fc.constants.FCHeaders;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Function Compute can answer invokeFunction for a function error with the 
status 200 and the X-Fc-Error-Type header.
+ * The SDK gives the header names in lower case, as Headers.toMultimap of 
OkHttp returns them.
+ */
+class InvokeFunctionErrorTest extends CamelTestSupport {
+
+    @BindToRegistry("fcClient")
+    Client fcClient = mock(Client.class);
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        String uri = 
"alibaba-fc:invokeFunction?serviceName=demo-service&functionName=demo-func&fcClient=#fcClient";
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:invoke")
+                        .to(uri);
+
+                from("direct:invokeTwice")
+                        .to(uri)
+                        .to("mock:first")
+                        .setBody(constant("{\"name\":\"camel\"}"))
+                        .to(uri)
+                        .to("mock:second");
+            }
+        };
+    }
+
+    @Test
+    void aFunctionErrorSetsTheFunctionErrorHeaderAndTheExchangeDoesNotFail() 
throws Exception {
+        when(fcClient.invokeFunction(anyString(), anyString(), 
any(InvokeFunctionRequest.class)))
+                .thenReturn(response("UnhandledInvocationError", 
"{\"errorMessage\":\"boom\"}"));
+
+        Exchange exchange = template.send("direct:invoke", e -> 
e.getMessage().setBody("{\"name\":\"camel\"}"));
+
+        assertThat(exchange.getException()).isNull();
+        
assertThat(exchange.getMessage().getHeader(FCHeaders.FUNCTION_ERROR)).isEqualTo("UnhandledInvocationError");
+        
assertThat(exchange.getMessage().getHeader(FCHeaders.STATUS_CODE)).isEqualTo(200);
+    }
+
+    @Test
+    void aSucceedingCallRemovesTheHeaderOfAnEarlierFunctionError() throws 
Exception {
+        when(fcClient.invokeFunction(anyString(), anyString(), 
any(InvokeFunctionRequest.class)))
+                .thenReturn(response("HandledInvocationError", 
"{\"errorMessage\":\"boom\"}"),
+                        response(null, "hello fc"));
+        MockEndpoint first = getMockEndpoint("mock:first");
+        first.expectedMessageCount(1);
+        MockEndpoint second = getMockEndpoint("mock:second");
+        second.expectedMessageCount(1);
+
+        template.sendBody("direct:invokeTwice", "{\"name\":\"camel\"}");
+
+        MockEndpoint.assertIsSatisfied(context);
+        
assertThat(first.getExchanges().get(0).getMessage().getHeader(FCHeaders.FUNCTION_ERROR))
+                .isEqualTo("HandledInvocationError");
+        
assertThat(second.getExchanges().get(0).getMessage().getHeaders()).doesNotContainKey(FCHeaders.FUNCTION_ERROR);
+    }
+
+    private static InvokeFunctionResponse response(String errorType, String 
body) {
+        Map<String, String> headers = new HashMap<>();
+        headers.put("x-fc-request-id", "request-1");
+        if (errorType != null) {
+            headers.put("x-fc-error-type", errorType);
+        }
+        return new InvokeFunctionResponse()
+                .setStatusCode(200)
+                .setHeaders(headers)
+                .setBody(body.getBytes(StandardCharsets.UTF_8));
+    }
+}
diff --git 
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/FCEndpointBuilderFactory.java
 
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/FCEndpointBuilderFactory.java
index f9c584b75903..e282bb6151dc 100644
--- 
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/FCEndpointBuilderFactory.java
+++ 
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/FCEndpointBuilderFactory.java
@@ -462,6 +462,22 @@ public interface FCEndpointBuilderFactory {
         public String alibabaFcStatusCode() {
             return "CamelAlibabaFcStatusCode";
         }
+        /**
+         * The error type that Function Compute returns with the status 200 for
+         * a function error, such as UnhandledInvocationError. The producer 
sets
+         * it for invokeFunction, or removes it if the response has none, 
unless
+         * the exchange fails. Such a function error does not fail the 
exchange,
+         * so a route detects it by this header.
+         * 
+         * The option is a: {@code String} type.
+         * 
+         * Group: producer
+         * 
+         * @return the name of the header {@code AlibabaFcFunctionError}.
+         */
+        public String alibabaFcFunctionError() {
+            return "CamelAlibabaFcFunctionError";
+        }
     }
     static FCEndpointBuilder endpointBuilder(String componentName, String 
path) {
         class FCEndpointBuilderImpl extends AbstractEndpointBuilder implements 
FCEndpointBuilder, AdvancedFCEndpointBuilder {

Reply via email to