You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2020/07/24 05:51:55 UTC

[camel] branch master updated: CAMEL-15326: fix incorrect handling of API responses (#4033)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 595f54f  CAMEL-15326: fix incorrect handling of API responses (#4033)
595f54f is described below

commit 595f54f2c9f4c0779394d02370efcad676c0646e
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Fri Jul 24 07:51:42 2020 +0200

    CAMEL-15326: fix incorrect handling of API responses (#4033)
    
    Includes:
    - fix a NPE thrown when the slack conversation.list request fails
    - report missing scopes when polling data from Slack
---
 .../camel/component/slack/SlackConsumer.java       | 25 ++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackConsumer.java b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackConsumer.java
index 18dfbaf..ca5e9b9 100644
--- a/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackConsumer.java
+++ b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackConsumer.java
@@ -26,6 +26,7 @@ import java.util.Queue;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.support.ScheduledBatchPollingConsumer;
 import org.apache.camel.util.CastUtils;
 import org.apache.camel.util.ObjectHelper;
@@ -74,6 +75,9 @@ public class SlackConsumer extends ScheduledBatchPollingConsumer {
         String jsonString = readResponse(response);
 
         JsonObject c = (JsonObject) Jsoner.deserialize(jsonString);
+
+        checkSlackReply(c);
+
         JsonArray list = c.getCollection("messages");
         exchanges = createExchanges(list);
         return processBatch(CastUtils.cast(exchanges));
@@ -133,7 +137,14 @@ public class SlackConsumer extends ScheduledBatchPollingConsumer {
 
         String jsonString = readResponse(response);
         JsonObject c = (JsonObject) Jsoner.deserialize(jsonString);
+
+        checkSlackReply(c);
+
         Collection<JsonObject> channels = c.getCollection("channels");
+        if (channels == null) {
+            throw new RuntimeCamelException("The response was successful but no channel list was provided");
+        }
+
         for (JsonObject singleChannel : channels) {
             if (singleChannel.get("name") != null) {
                 if (singleChannel.get("name").equals(channel)) {
@@ -147,4 +158,18 @@ public class SlackConsumer extends ScheduledBatchPollingConsumer {
         return jsonString;
     }
 
+    private void checkSlackReply(JsonObject c) {
+        boolean okStatus = c.getBoolean("ok");
+
+        if (!okStatus) {
+            String errorMessage = c.getString("error");
+
+            if (errorMessage == null || errorMessage.isEmpty()) {
+                errorMessage = "the slack server did not provide error details";
+            }
+
+            throw new RuntimeCamelException(String.format("API request to Slack failed: %s", errorMessage));
+        }
+    }
+
 }