gnodet-bot commented on code in PR #31080:
URL: https://github.com/apache/camel/pull/31080#discussion_r4727936376


##########
components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSProducer.java:
##########
@@ -69,12 +75,36 @@ private void sendSms(Exchange exchange, 
ClientConfigurations configuration, Clie
         }
 
         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())) {
             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", String.valueOf(data.get("Code")),

Review Comment:
   ⚠️ **`String.valueOf(null)` returns the literal string `"null"`**, not 
`null`. When `Code` is absent from the response (body is null, or body has no 
`Code` field), `data.get("Code")` returns `null` after `removeIf`, so 
`TeaException.getCode()` returns `"null"` — a confusing sentinel. A caller 
switching on `getCode()` will never match a `null` case; they'd have to know to 
check for `"null"` as a string. Use `"UNKNOWN"` as the fallback instead:
   
   ```suggestion
                           "code", data.containsKey("Code") ? 
data.get("Code").toString() : "UNKNOWN",
   ```



##########
components/camel-alibaba/camel-alibaba-sms/src/main/java/org/apache/camel/component/alibaba/sms/SMSProducer.java:
##########
@@ -69,12 +75,36 @@ private void sendSms(Exchange exchange, 
ClientConfigurations configuration, Clie
         }
 
         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())) {
             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", String.valueOf(data.get("Code")),
+                        "message", "code: " + response.getStatusCode() + ", " 
+ data.get("Message") + " request id: "
+                                   + data.get("RequestId"),

Review Comment:
   ⚠️ **Null message fields produce `"null"` literals in the exception 
message.** After `removeIf(Objects::isNull)`, `data.get("Message")` and 
`data.get("RequestId")` return `null` when absent (key not present). Java 
string concatenation turns these into the literal `"null"`, yielding messages 
like `"code: 200, null request id: null"`. The 
`aResponseWithoutBodyFailsTheExchange` test does not assert on `getMessage()` 
or `getCode()`, so this passes today.
   
   Use `Objects.toString` with a fallback:
   
   ```suggestion
                           "message", "code: " + response.getStatusCode() + ", "
                                      + Objects.toString(data.get("Message"), 
"(no message)")
                                      + " request id: " + 
Objects.toString(data.get("RequestId"), "(none)"),
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to