From b65b7aa07ff05da1c7d5c4b2b6ab7376160fe35a Mon Sep 17 00:00:00 2001 From: Stuart Lang Date: Tue, 19 May 2026 03:03:58 +0100 Subject: [PATCH] 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. --- .../hectorvent/floci/services/sns/SnsService.java | 12 +++++++----- .../floci/services/sns/SnsServiceTest.java | 9 +++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java index ac20180ba1..d32d6736e3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java @@ -296,9 +296,10 @@ public class SnsService { public String publish(String topicArn, String targetArn, String phoneNumber, String message, String subject, Map 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 attrs = (Map) 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 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; } diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java index 1c78e04671..71f146c7fd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java @@ -223,17 +223,17 @@ class SnsServiceTest { } @Test - void publish_messageExceedsSizeLimit_throwsInvalidParameterException() { + void publish_messageExceedsSizeLimit_throwsInvalidParameter() { Topic topic = snsService.createTopic("size-topic", null, null, REGION); String tooBig = "x".repeat(262_145); AwsException ex = assertThrows(AwsException.class, () -> snsService.publish(topic.getTopicArn(), null, tooBig, null, REGION)); - assertEquals("InvalidParameterException", ex.getErrorCode()); + assertEquals("InvalidParameter", ex.getErrorCode()); assertTrue(ex.getMessage().toLowerCase().contains("too long")); } @Test - void publish_messagePlusAttributesExceedsLimit_throws() { + void publish_messagePlusAttributesExceedsLimit_throwsInvalidParameter() { Topic topic = snsService.createTopic("size-topic", null, null, REGION); String body = "x".repeat(262_100); Map attrs = Map.of( @@ -241,7 +241,8 @@ class SnsServiceTest { "y".repeat(200), "String")); AwsException ex = assertThrows(AwsException.class, () -> snsService.publish(topic.getTopicArn(), null, null, body, null, attrs, REGION)); - assertEquals("InvalidParameterException", ex.getErrorCode()); + assertEquals("InvalidParameter", ex.getErrorCode()); + assertTrue(ex.getMessage().toLowerCase().contains("too long")); } @Test