fix(sns): return per-entry failures from PublishBatch instead of aborting whole batch (#1008)

Real AWS SNS PublishBatch surfaces an attribute parsing error
(e.g. invalid base64 in a BinaryValue) as a Failed entry with
InvalidParameterValue + SenderFault=true, and continues publishing
the remaining valid entries.

Both SnsQueryHandler and SnsJsonHandler used to let the AwsException
from parseMessageAttributes propagate out of the batch loop, returning
a 400 (Query) or letting the exception bubble (JSON) and aborting the
whole batch. They now catch the per-entry parse failure, record it as
a Failed entry, and merge it with the regular batch result.

Fix #992
This commit is contained in:
Ben Younes authored and GitHub committed 2026-05-23 22:27:50 -05:00
1 parent 6434fc91f7
commit f5dba627be
4 files changed
+168 -5

No files matched your search

@@ -209,12 +209,14 @@ public class SnsJsonHandler {
private Response handlePublishBatch(JsonNode request, String region) {
String topicArn = request.path("TopicArn").asText(null);
List<Map<String, Object>> entries = new ArrayList<>();
List<String[]> entryAttributeFailures = new ArrayList<>();
JsonNode entriesNode = request.path("PublishBatchRequestEntries");
if (entriesNode.isArray()) {
for (JsonNode entryNode : entriesNode) {
String id = entryNode.path("Id").asText(null);
Map<String, Object> entry = new HashMap<>();
entry.put("Id", entryNode.path("Id").asText(null));
entry.put("Id", id);
entry.put("Message", entryNode.path("Message").asText(null));
entry.put("Subject", entryNode.path("Subject").asText(null));
entry.put("MessageGroupId", entryNode.path("MessageGroupId").asText(null));
@@ -222,7 +224,14 @@ public class SnsJsonHandler {
JsonNode attrsNode = entryNode.path("MessageAttributes");
if (attrsNode.isObject()) {
entry.put("MessageAttributes", parseMessageAttributes(attrsNode));
try {
entry.put("MessageAttributes", parseMessageAttributes(attrsNode));
} catch (AwsException e) {
// Real AWS SNS surfaces per-entry attribute errors as Failed entries
// and keeps processing the rest of the batch, instead of aborting.
entryAttributeFailures.add(new String[]{id, e.getErrorCode(), e.getMessage(), "true"});
continue;
}
}
entries.add(entry);
@@ -240,7 +249,9 @@ public class SnsJsonHandler {
successful.add(item);
}
ArrayNode failed = response.putArray("Failed");
for (String[] f : result.failed()) {
List<String[]> allFailed = new ArrayList<>(result.failed());
allFailed.addAll(entryAttributeFailures);
for (String[] f : allFailed) {
ObjectNode item = objectMapper.createObjectNode();
item.put("Id", f[0]);
item.put("Code", f[1]);
@@ -205,6 +205,7 @@ public class SnsQueryHandler {
private Response handlePublishBatch(MultivaluedMap<String, String> params, String region) {
String topicArn = getParam(params, "TopicArn");
List<Map<String, Object>> entries = new ArrayList<>();
List<String[]> entryAttributeFailures = new ArrayList<>();
for (int i = 1; ; i++) {
String entryPrefix = "PublishBatchRequestEntries.member." + i;
String id = getParam(params, entryPrefix + ".Id");
@@ -220,7 +221,10 @@ public class SnsQueryHandler {
try {
attributes = parseMessageAttributes(params, entryPrefix + ".MessageAttributes.entry.");
} catch (AwsException e) {
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
// Real AWS SNS surfaces per-entry attribute errors as Failed entries
// and keeps processing the rest of the batch, instead of aborting.
entryAttributeFailures.add(new String[]{id, e.getErrorCode(), e.getMessage(), "true"});
continue;
}
if (!attributes.isEmpty()) entry.put("MessageAttributes", attributes);
entries.add(entry);
@@ -233,7 +237,9 @@ public class SnsQueryHandler {
xml.start("member").elem("Id", s[0]).elem("MessageId", s[1]).end("member");
}
xml.end("Successful").start("Failed");
for (String[] f : result.failed()) {
List<String[]> allFailed = new ArrayList<>(result.failed());
allFailed.addAll(entryAttributeFailures);
for (String[] f : allFailed) {
xml.start("member").elem("Id", f[0]).elem("Code", f[1])
.elem("Message", f[2]).elem("SenderFault", f[3]).end("member");
}