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

@@ -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<String, io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue> 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