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 61a2973f7061 CAMEL-27756: camel-alibaba-sms - fail sendSms for a Code 
other than OK (#31080)
61a2973f7061 is described below

commit 61a2973f7061501bb613af083879d183a701798c
Author: Saravanakumar Selvaraj <[email protected]>
AuthorDate: Mon Nov 30 11:40:53 2026 +0000

    CAMEL-27756: camel-alibaba-sms - fail sendSms for a Code other than OK 
(#31080)
    
    Short Message Service gives the result of SendSms in the Code field, and 
only OK means success. Some error codes come
    with the HTTP status 200. tea-openapi throws a TeaException only for an 
HTTP status from 400 to 599, and SMSProducer
    never checked the Code. So a failed send could complete the exchange, and 
only the code key of the body showed the
    error.
    
    sendSms now fails the exchange with a TeaException for a response with a 
Code other than OK. A response without a Code
    or without a body fails it too. The exception has the form that tea-openapi 
builds for an HTTP status from 400 to 599.
    getCode() gives the Code, and getData() holds the fields of the body and 
the statusCode. So one
    onException(TeaException.class) handles a Code other than OK and an HTTP 
status from 400 to 599. The producerOut text
    and the doc page say when the exchange fails.
    
    A response with the Code OK gives the same Map body and the same 
CamelAlibabaSmsRequestId header as before. The
    component JSON, its catalog copy and the catalog copy of the doc page are 
regenerated. Three new tests of SendSmsTest
    cover the three cases. SMSProducerTest now mocks a response with the Code 
OK.
    
    The upgrade guide of 4.25 has a section on the change.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/catalog/components/alibaba-sms.json      |  2 +-
 .../camel/catalog/docs/alibaba-sms-component.adoc  |  6 +++
 .../camel/component/alibaba/sms/alibaba-sms.json   |  2 +-
 .../src/main/docs/alibaba-sms-component.adoc       |  6 +++
 .../camel/component/alibaba/sms/SMSEndpoint.java   |  3 +-
 .../camel/component/alibaba/sms/SMSProducer.java   | 33 +++++++++++-
 .../component/alibaba/sms/SMSProducerTest.java     |  3 +-
 .../camel/component/alibaba/sms/SendSmsTest.java   | 63 ++++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc    | 16 ++++++
 9 files changed, 129 insertions(+), 5 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/alibaba-sms.json
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/alibaba-sms.json
index 84090ca4eb24..31ead0d1ed72 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/alibaba-sms.json
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/alibaba-sms.json
@@ -40,7 +40,7 @@
   },
   "bodyTypes": {
     "producerIn": { "carriers": [ "any" ], "description": "Not used: sendSms 
takes the phone numbers, sign name, template code and template parameters from 
options or headers, such as CamelAlibabaSmsTemplateParam" },
-    "producerOut": { "carriers": [ "map" ], "description": "A Map of the 
response: the statusCode, and the code, message, bizId and requestId the 
service returns" }
+    "producerOut": { "carriers": [ "map" ], "description": "A Map of the 
response: the statusCode, and the code, message, bizId and requestId the 
service returns. The producer fails the exchange with a 
com.aliyun.tea.TeaException for a Code other than OK, or none" }
   },
   "properties": {
     "operation": { "index": 0, "kind": "path", "displayName": "Operation", 
"group": "producer", "label": "producer", "required": true, "type": "enum", 
"javaType": "java.lang.String", "enum": [ "sendSms" ], "deprecated": false, 
"autowired": false, "secret": false, "description": "The operation that the 
producer performs. The CamelAlibabaSmsOperation header replaces this option for 
a request that the producer sends. If that header is not set, an exchange 
property of the same name replaces  [...]
diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-sms-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-sms-component.adoc
index fd4f533f787a..5cdd36d10288 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-sms-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-sms-component.adoc
@@ -50,6 +50,12 @@ The component supports the following operations:
 
 * `sendSms` - send an SMS message (producer)
 
+=== Result of sendSms
+
+Only the `Code` `OK` means success, so the producer fails the exchange for any 
other `Code`, or none. It throws a
+`com.aliyun.tea.TeaException` in the form that the SDK throws for an HTTP 
status from 400 to 599. `getCode()` of the
+exception gives the `Code`, and `getData()` holds the fields of the response, 
such as `BizId` and `RequestId`.
+
 === Producer example
 
 [tabs]
diff --git 
a/components/camel-alibaba/camel-alibaba-sms/src/generated/resources/META-INF/org/apache/camel/component/alibaba/sms/alibaba-sms.json
 
b/components/camel-alibaba/camel-alibaba-sms/src/generated/resources/META-INF/org/apache/camel/component/alibaba/sms/alibaba-sms.json
index 84090ca4eb24..31ead0d1ed72 100644
--- 
a/components/camel-alibaba/camel-alibaba-sms/src/generated/resources/META-INF/org/apache/camel/component/alibaba/sms/alibaba-sms.json
+++ 
b/components/camel-alibaba/camel-alibaba-sms/src/generated/resources/META-INF/org/apache/camel/component/alibaba/sms/alibaba-sms.json
@@ -40,7 +40,7 @@
   },
   "bodyTypes": {
     "producerIn": { "carriers": [ "any" ], "description": "Not used: sendSms 
takes the phone numbers, sign name, template code and template parameters from 
options or headers, such as CamelAlibabaSmsTemplateParam" },
-    "producerOut": { "carriers": [ "map" ], "description": "A Map of the 
response: the statusCode, and the code, message, bizId and requestId the 
service returns" }
+    "producerOut": { "carriers": [ "map" ], "description": "A Map of the 
response: the statusCode, and the code, message, bizId and requestId the 
service returns. The producer fails the exchange with a 
com.aliyun.tea.TeaException for a Code other than OK, or none" }
   },
   "properties": {
     "operation": { "index": 0, "kind": "path", "displayName": "Operation", 
"group": "producer", "label": "producer", "required": true, "type": "enum", 
"javaType": "java.lang.String", "enum": [ "sendSms" ], "deprecated": false, 
"autowired": false, "secret": false, "description": "The operation that the 
producer performs. The CamelAlibabaSmsOperation header replaces this option for 
a request that the producer sends. If that header is not set, an exchange 
property of the same name replaces  [...]
diff --git 
a/components/camel-alibaba/camel-alibaba-sms/src/main/docs/alibaba-sms-component.adoc
 
b/components/camel-alibaba/camel-alibaba-sms/src/main/docs/alibaba-sms-component.adoc
index fd4f533f787a..5cdd36d10288 100644
--- 
a/components/camel-alibaba/camel-alibaba-sms/src/main/docs/alibaba-sms-component.adoc
+++ 
b/components/camel-alibaba/camel-alibaba-sms/src/main/docs/alibaba-sms-component.adoc
@@ -50,6 +50,12 @@ The component supports the following operations:
 
 * `sendSms` - send an SMS message (producer)
 
+=== Result of sendSms
+
+Only the `Code` `OK` means success, so the producer fails the exchange for any 
other `Code`, or none. It throws a
+`com.aliyun.tea.TeaException` in the form that the SDK throws for an HTTP 
status from 400 to 599. `getCode()` of the
+exception gives the `Code`, and `getData()` holds the fields of the response, 
such as `BizId` and `RequestId`.
+
 === Producer example
 
 [tabs]
diff --git 
a/components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSEndpoint.java
 
b/components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSEndpoint.java
index b97888c18d58..263a4200daba 100644
--- 
a/components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSEndpoint.java
+++ 
b/components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSEndpoint.java
@@ -44,7 +44,8 @@ import org.apache.camel.support.DefaultEndpoint;
                                                 + " 
CamelAlibabaSmsTemplateParam"),
            producerOut = @BodyType(value = Carrier.MAP,
                                    description = "A Map of the response: the 
statusCode, and the code, message, bizId and"
-                                                 + " requestId the service 
returns"))
+                                                 + " requestId the service 
returns. The producer fails the exchange with a"
+                                                 + " 
com.aliyun.tea.TeaException for a Code other than OK, or none"))
 public class SMSEndpoint extends DefaultEndpoint {
 
     @UriPath(description = "The operation that the producer performs. The 
CamelAlibabaSmsOperation header replaces this option"
diff --git 
a/components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSProducer.java
 
b/components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSProducer.java
index 338ac5b265b2..c5ba5bbed233 100644
--- 
a/components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSProducer.java
+++ 
b/components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSProducer.java
@@ -16,9 +16,15 @@
  */
 package org.apache.camel.component.alibaba.sms;
 
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
 import com.aliyun.dysmsapi20170525.Client;
 import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
 import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
+import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
+import com.aliyun.tea.TeaException;
 import org.apache.camel.Exchange;
 import org.apache.camel.component.alibaba.sms.constants.SMSHeaders;
 import org.apache.camel.component.alibaba.sms.constants.SMSOperations;
@@ -69,12 +75,37 @@ public class SMSProducer extends DefaultProducer {
         }
 
         SendSmsResponse response = smsClient.sendSms(request);
+        // only the Code OK means success, and tea-openapi does not check the 
Code
+        if (response.getBody() == null || 
!"OK".equals(response.getBody().getCode())) {
+            throw sendSmsError(response);
+        }
         exchange.getMessage().setBody(SMSUtils.toSendSmsMap(response));
-        if (response.getBody() != null && 
ObjectHelper.isNotEmpty(response.getBody().getRequestId())) {
+        if (ObjectHelper.isNotEmpty(response.getBody().getRequestId())) {
             exchange.getMessage().setHeader(SMSHeaders.REQUEST_ID, 
response.getBody().getRequestId());
         }
     }
 
+    /**
+     * Builds a TeaException in the form that tea-openapi throws for an HTTP 
status from 400 to 599. So one onException
+     * handles a Code other than OK and such a status alike.
+     */
+    private static TeaException sendSmsError(SendSmsResponse response) {
+        SendSmsResponseBody body = response.getBody();
+        Map<String, Object> data = body != null ? new HashMap<>(body.toMap()) 
: new HashMap<>();
+        // as in the JSON of a response, a field that the service did not send 
has no key
+        data.values().removeIf(Objects::isNull);
+        if (response.getStatusCode() != null) {
+            data.put("statusCode", response.getStatusCode());
+        }
+        return new TeaException(
+                Map.of(
+                        "code", data.containsKey("Code") ? 
data.get("Code").toString() : "UNKNOWN",
+                        "message", "code: " + response.getStatusCode() + ", "
+                                   + Objects.toString(data.get("Message"), 
"(no message)") + " request id: "
+                                   + Objects.toString(data.get("RequestId"), 
"(none)"),
+                        "data", data));
+    }
+
     @Override
     public SMSEndpoint getEndpoint() {
         return (SMSEndpoint) super.getEndpoint();
diff --git 
a/components/camel-alibaba/camel-alibaba-sms/src/test/java/org/apache/camel/component/alibaba/sms/SMSProducerTest.java
 
b/components/camel-alibaba/camel-alibaba-sms/src/test/java/org/apache/camel/component/alibaba/sms/SMSProducerTest.java
index 24c874faa826..65e94671346f 100644
--- 
a/components/camel-alibaba/camel-alibaba-sms/src/test/java/org/apache/camel/component/alibaba/sms/SMSProducerTest.java
+++ 
b/components/camel-alibaba/camel-alibaba-sms/src/test/java/org/apache/camel/component/alibaba/sms/SMSProducerTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.alibaba.sms;
 import com.aliyun.dysmsapi20170525.Client;
 import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
 import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
+import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
 import org.apache.camel.support.DefaultExchange;
 import org.apache.camel.test.junit6.CamelTestSupport;
 import org.junit.jupiter.api.Test;
@@ -43,7 +44,7 @@ class SMSProducerTest extends CamelTestSupport {
         Client secondClient = mock(Client.class);
         when(endpoint.initClient()).thenReturn(firstClient, secondClient);
 
-        SendSmsResponse response = new SendSmsResponse();
+        SendSmsResponse response = new SendSmsResponse().setBody(new 
SendSmsResponseBody().setCode("OK"));
         
when(firstClient.sendSms(any(SendSmsRequest.class))).thenReturn(response);
         
when(secondClient.sendSms(any(SendSmsRequest.class))).thenReturn(response);
 
diff --git 
a/components/camel-alibaba/camel-alibaba-sms/src/test/java/org/apache/camel/component/alibaba/sms/SendSmsTest.java
 
b/components/camel-alibaba/camel-alibaba-sms/src/test/java/org/apache/camel/component/alibaba/sms/SendSmsTest.java
index c3c3755aa0a5..5eef8f66d8ef 100644
--- 
a/components/camel-alibaba/camel-alibaba-sms/src/test/java/org/apache/camel/component/alibaba/sms/SendSmsTest.java
+++ 
b/components/camel-alibaba/camel-alibaba-sms/src/test/java/org/apache/camel/component/alibaba/sms/SendSmsTest.java
@@ -22,6 +22,7 @@ import com.aliyun.dysmsapi20170525.Client;
 import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
 import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
 import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
+import com.aliyun.tea.TeaException;
 import org.apache.camel.BindToRegistry;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
@@ -93,4 +94,66 @@ class SendSmsTest extends CamelTestSupport {
 
         verify(smsClient).sendSms(any(SendSmsRequest.class));
     }
+
+    @Test
+    void aCodeOtherThanOkFailsTheExchangeWithATeaException() throws Exception {
+        
when(smsClient.sendSms(any(SendSmsRequest.class))).thenReturn(response(new 
SendSmsResponseBody()
+                .setCode("InternalError")
+                .setMessage("The request processing has failed due to some 
unknown error, exception or failure.")
+                .setBizId("biz-123")
+                .setRequestId("req-456")));
+
+        Exchange exchange = template.send("direct:send", e -> 
e.getMessage().setBody("ignored"));
+
+        assertThat(exchange.getException()).as("the exception of the exchange 
for the Code InternalError")
+                .isInstanceOf(TeaException.class);
+        TeaException error = exchange.getException(TeaException.class);
+        assertThat(error.getCode()).isEqualTo("InternalError");
+        assertThat(error.getMessage()).isEqualTo("code: 200, The request 
processing has failed due to some unknown"
+                                                 + " error, exception or 
failure. request id: req-456");
+        assertThat(error.getStatusCode()).isEqualTo(200);
+        assertThat(error.getData())
+                .containsEntry("Code", "InternalError")
+                .containsEntry("BizId", "biz-123")
+                .containsEntry("RequestId", "req-456")
+                .containsEntry("statusCode", 200);
+        
assertThat(exchange.getMessage().getHeader(SMSHeaders.REQUEST_ID)).isNull();
+        
assertThat(getMockEndpoint("mock:result").getReceivedCounter()).isZero();
+    }
+
+    @Test
+    void aResponseWithoutCodeFailsTheExchange() throws Exception {
+        when(smsClient.sendSms(any(SendSmsRequest.class)))
+                .thenReturn(response(new 
SendSmsResponseBody().setRequestId("req-456")));
+
+        Exchange exchange = template.send("direct:send", e -> 
e.getMessage().setBody("ignored"));
+
+        assertThat(exchange.getException()).as("the exception of the exchange 
for a response without a Code")
+                .isInstanceOf(TeaException.class);
+        assertThat(exchange.getException(TeaException.class).getData())
+                .containsOnlyKeys("RequestId", "statusCode")
+                .containsEntry("RequestId", "req-456");
+        
assertThat(exchange.getException(TeaException.class).getCode()).isEqualTo("UNKNOWN");
+        assertThat(exchange.getException(TeaException.class).getMessage())
+                .isEqualTo("code: 200, (no message) request id: req-456");
+    }
+
+    @Test
+    void aResponseWithoutBodyFailsTheExchange() throws Exception {
+        
when(smsClient.sendSms(any(SendSmsRequest.class))).thenReturn(response(null));
+
+        Exchange exchange = template.send("direct:send", e -> 
e.getMessage().setBody("ignored"));
+
+        assertThat(exchange.getException()).as("the exception of the exchange 
for a response without a body")
+                .isInstanceOf(TeaException.class);
+        
assertThat(exchange.getException(TeaException.class).getStatusCode()).isEqualTo(200);
+        
assertThat(exchange.getException(TeaException.class).getData()).containsOnlyKeys("statusCode");
+        
assertThat(exchange.getException(TeaException.class).getCode()).isEqualTo("UNKNOWN");
+        assertThat(exchange.getException(TeaException.class).getMessage())
+                .isEqualTo("code: 200, (no message) request id: (none)");
+    }
+
+    private static SendSmsResponse response(SendSmsResponseBody body) {
+        return new SendSmsResponse().setStatusCode(200).setBody(body);
+    }
 }
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
index 31b03928009f..84a8f4a44912 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
@@ -33,3 +33,19 @@ it sent them over HTTP, the protocol that 
`EventBridgeClient` of the SDK uses wh
 
 A route whose `endpoint` option names a host that accepts only HTTP needs 
`tls=false` now. A client that is set or
 autowired in the `eventBridgeClient` option keeps its own protocol.
+
+=== camel-alibaba-sms - sendSms fails the exchange for a Code other than OK
+
+The `sendSms` operation of `alibaba-sms` now fails the exchange for a response 
with a `Code` other than `OK`. A
+response without a `Code` or without a body fails the exchange too. Short 
Message Service gives some error codes, such
+as `InternalError`, the HTTP status 200. Only the `Code` `OK` means success. 
Before, the exchange completed, and the
+`code` key of the body held the error code.
+
+The exception is a `com.aliyun.tea.TeaException` in the form that the SDK 
throws for an HTTP status from 400 to 599.
+`getCode()` gives the `Code`, `getStatusCode()` the HTTP status, and 
`getData()` the fields of the response, such as
+`BizId` and `RequestId`. So one `onException(TeaException.class)` handles a 
`Code` other than `OK` and an HTTP status
+from 400 to 599. The body then stays as it was, and the producer does not set 
the `CamelAlibabaSmsRequestId` header.
+
+A route that tests the `code` key of the body for an error needs 
`onException`, or `doTry` and `doCatch`, now. An error
+handler that redelivers now sends such a request again. A test that mocks the 
client must return a body with the `Code`
+`OK` for a send that succeeds.

Reply via email to