This is an automated email from the ASF dual-hosted git repository.
acosentino pushed a commit to branch camel-3.4.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.4.x by this push:
new 050ed1b CAMEL-15326: fix incorrect handling of API responses (#4033)
(#4041)
050ed1b is described below
commit 050ed1b58c3fc3eb547ad010081f26299da15594
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Fri Jul 24 10:22:28 2020 +0200
CAMEL-15326: fix incorrect handling of API responses (#4033) (#4041)
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));
+ }
+ }
+
}