Skip to content

Commit

Permalink
feat(sdk): queue.push() can now handle invalid messages for aws tar…
Browse files Browse the repository at this point in the history
…gets (#3816)

Added error handling for invalid messages in `Queue.push()` following [AWS documentation](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html).

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
garysassano authored Aug 15, 2023
1 parent ecb5bc8 commit 207a338
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
22 changes: 17 additions & 5 deletions libs/wingsdk/src/shared-aws/queue.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PurgeQueueCommand,
GetQueueAttributesCommand,
ReceiveMessageCommand,
InvalidMessageContents,
} from "@aws-sdk/client-sqs";
import { IQueueClient } from "../cloud";

Expand All @@ -14,11 +15,22 @@ export class QueueClient implements IQueueClient {
) {}

public async push(message: string): Promise<void> {
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<void> {
Expand Down
42 changes: 42 additions & 0 deletions libs/wingsdk/test/shared-aws/queue.inflight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 207a338

Please sign in to comment.