fix(sns): emit canonical error codes for Publish size violations (#906)

* fix(sns): emit correct error codes for Publish size violations

Publish was throwing AwsException with the SDK class name
"InvalidParameterException" as the error code, which the AWS SDK
unmarshaller doesn't recognise (it expects the AWS Query protocol
code "InvalidParameter"). Result: the SDK fell back to the base
AmazonSimpleNotificationServiceException instead of the typed
InvalidParameterException.

Use the canonical AWS error codes:
- body alone > 256 KiB -> "InvalidParameter" (-> InvalidParameterException)
- body + subject + attributes > 256 KiB -> "InvalidParameterValue"
  (-> InvalidParameterValueException), matching real SNS which
  blames the attribute set when the body itself fits.

* refactor(sns): avoid double UTF-8 encoding of message in publish size check

Reuse the precomputed messageBytes when calling computePublishSize so
the message body isn't encoded twice for the body-only and combined
payload checks.

* fix(sns): align both Publish size-violation branches to InvalidParameter

Review feedback on #906: validation against real AWS SNS (eu-west-1)
showed both the body-only and combined-payload overflow cases return
the same error: InvalidParameter / "Invalid parameter: Message too long".
The two-code split in the PR followed the SDK documentation but diverged
from observed wire behaviour.

Collapse to a single check using the already-computed payload size, and
drop the misleading "MessageAttributes Reason: ..." message text since
the overflow can come from any combination of body, subject, or
attributes. Update the corresponding test name and assertion.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-05-18 21:03:58 -05:00
1 parent aec85f993a
commit b65b7aa07f
2 files changed
+12 -9

No files matched your search

@@ -296,9 +296,10 @@ public class SnsService {
public String publish(String topicArn, String targetArn, String phoneNumber, String message,
String subject, Map<String, MessageAttributeValue> messageAttributes,
String messageGroupId, String messageDeduplicationId, String region) {
int payloadSize = computePublishSize(message, subject, messageAttributes);
int messageBytes = message == null ? 0 : message.getBytes(StandardCharsets.UTF_8).length;
int payloadSize = computePublishSize(messageBytes, subject, messageAttributes);
if (payloadSize > MAX_PUBLISH_SIZE) {
throw new AwsException("InvalidParameterException",
throw new AwsException("InvalidParameter",
"Invalid parameter: Message too long", 400);
}
@@ -398,7 +399,8 @@ public class SnsService {
@SuppressWarnings("unchecked")
Map<String, MessageAttributeValue> attrs =
(Map<String, MessageAttributeValue>) entry.get("MessageAttributes");
batchSize += computePublishSize(message, subject, attrs);
int entryMessageBytes = message == null ? 0 : message.getBytes(StandardCharsets.UTF_8).length;
batchSize += computePublishSize(entryMessageBytes, subject, attrs);
}
if (batchSize > MAX_PUBLISH_SIZE) {
throw new AwsException("BatchRequestTooLong",
@@ -1103,9 +1105,9 @@ public class SnsService {
return AwsArnUtils.arnToQueueUrl(arn, baseUrl);
}
private static int computePublishSize(String message, String subject,
private static int computePublishSize(int messageBytes, String subject,
Map<String, MessageAttributeValue> attributes) {
int total = message == null ? 0 : message.getBytes(StandardCharsets.UTF_8).length;
int total = messageBytes;
if (subject != null) {
total += subject.getBytes(StandardCharsets.UTF_8).length;
}