-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (41 loc) · 1.4 KB
/
index.js
File metadata and controls
50 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
// SPDX-License-Identifier: MIT
const defaults = {
logger: console.error,
};
const sqsPartialBatchFailureMiddleware = (opts = {}) => {
const { logger } = { ...defaults, ...opts };
const sqsPartialBatchFailureMiddlewareAfter = (request) => {
const {
event: { Records },
response,
} = request;
// https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html
// Required: include the value `ReportBatchItemFailures` in the `FunctionResponseTypes` list
const batchItemFailures = [];
if (Array.isArray(Records)) {
for (const [idx, record] of Records.entries()) {
const { status, reason } = response[idx];
if (status === "fulfilled") continue;
batchItemFailures.push({ itemIdentifier: record.messageId });
if (typeof logger === "function") {
logger(reason, record);
}
}
}
request.response = { batchItemFailures };
};
const sqsPartialBatchFailureMiddlewareOnError = async (request) => {
if (typeof request.response !== "undefined") return;
request.response = new Array(request.event.Records?.length ?? 0).fill({
status: "rejected",
reason: request.error,
});
await sqsPartialBatchFailureMiddlewareAfter(request);
};
return {
after: sqsPartialBatchFailureMiddlewareAfter,
onError: sqsPartialBatchFailureMiddlewareOnError,
};
};
export default sqsPartialBatchFailureMiddleware;