diff --git a/libs/wingsdk/src/shared-aws/queue.inflight.ts b/libs/wingsdk/src/shared-aws/queue.inflight.ts index 7001687a18b..f8c0b59fbb0 100644 --- a/libs/wingsdk/src/shared-aws/queue.inflight.ts +++ b/libs/wingsdk/src/shared-aws/queue.inflight.ts @@ -4,6 +4,7 @@ import { PurgeQueueCommand, GetQueueAttributesCommand, ReceiveMessageCommand, + InvalidMessageContents, } from "@aws-sdk/client-sqs"; import { IQueueClient } from "../cloud"; @@ -14,11 +15,22 @@ export class QueueClient implements IQueueClient { ) {} public async push(message: string): Promise { - const command = new SendMessageCommand({ - QueueUrl: this.queueUrl, - MessageBody: message, - }); - await this.client.send(command); + try { + const command = new SendMessageCommand({ + QueueUrl: this.queueUrl, + MessageBody: message, + }); + await this.client.send(command); + } catch (e) { + if (e instanceof InvalidMessageContents) { + throw new Error( + `The message contains characters outside the allowed set (message=${message}): ${ + (e as Error).stack + })}` + ); + } + throw new Error((e as Error).stack); + } } public async purge(): Promise { diff --git a/libs/wingsdk/test/shared-aws/queue.inflight.test.ts b/libs/wingsdk/test/shared-aws/queue.inflight.test.ts index dd98725c0e0..61475ab858e 100644 --- a/libs/wingsdk/test/shared-aws/queue.inflight.test.ts +++ b/libs/wingsdk/test/shared-aws/queue.inflight.test.ts @@ -4,6 +4,7 @@ import { GetQueueAttributesCommand, SQSClient, ReceiveMessageCommand, + InvalidMessageContents, } from "@aws-sdk/client-sqs"; import { mockClient } from "aws-sdk-client-mock"; import { test, expect, beforeEach } from "vitest"; @@ -35,6 +36,47 @@ test("push - happy path", async () => { expect(response).toEqual(undefined); }); +test("push - sad path invalid message", async () => { + // GIVEN + const QUEUE_URL = "QUEUE_URL"; + const MESSAGE = "INVALID_MESSAGE"; + + sqsMock + .on(SendMessageCommand, { QueueUrl: QUEUE_URL, MessageBody: MESSAGE }) + .rejects( + new InvalidMessageContents({ + message: "InvalidMessageContents error", + $metadata: {}, + }) + ); + + // WHEN + const client = new QueueClient(QUEUE_URL); + + // THEN + await expect(() => client.push(MESSAGE)).rejects.toThrowError( + /The message contains characters outside the allowed set/ + ); +}); + +test("push - sad path unknown error", async () => { + // GIVEN + const QUEUE_URL = "QUEUE_URL"; + const MESSAGE = "MESSAGE"; + + sqsMock + .on(SendMessageCommand, { QueueUrl: QUEUE_URL, MessageBody: MESSAGE }) + .rejects(new Error("unknown error")); + + // WHEN + const client = new QueueClient(QUEUE_URL); + + // THEN + await expect(() => client.push(MESSAGE)).rejects.toThrowError( + /unknown error/ + ); +}); + test("purge - happy path", async () => { // GIVEN const QUEUE_URL = "QUEUE_URL";