@adobe/spacecat-shared-utils 1.26.2 → 1.26.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/sqs.js +24 -16
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [@adobe/spacecat-shared-utils-v1.26.3](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.26.2...@adobe/spacecat-shared-utils-v1.26.3) (2025-01-15)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* improve sqs event adapter error handling ([#541](https://github.com/adobe/spacecat-shared/issues/541)) ([2e9fca8](https://github.com/adobe/spacecat-shared/commit/2e9fca89e327b9b680b20a6105088f0e844eb54a))
|
|
7
|
+
|
|
1
8
|
# [@adobe/spacecat-shared-utils-v1.26.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.26.1...@adobe/spacecat-shared-utils-v1.26.2) (2025-01-12)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/sqs.js
CHANGED
|
@@ -12,7 +12,15 @@
|
|
|
12
12
|
|
|
13
13
|
import { SendMessageCommand, SQSClient } from '@aws-sdk/client-sqs';
|
|
14
14
|
import AWSXray from 'aws-xray-sdk';
|
|
15
|
-
|
|
15
|
+
|
|
16
|
+
import { hasText, isNonEmptyArray } from './functions.js';
|
|
17
|
+
|
|
18
|
+
function badRequest(message) {
|
|
19
|
+
return new Response('', {
|
|
20
|
+
status: 400,
|
|
21
|
+
headers: { 'x-error': message },
|
|
22
|
+
});
|
|
23
|
+
}
|
|
16
24
|
|
|
17
25
|
/**
|
|
18
26
|
* @class SQS utility to send messages to SQS
|
|
@@ -94,24 +102,24 @@ export function sqsEventAdapter(fn) {
|
|
|
94
102
|
const { log } = context;
|
|
95
103
|
let message;
|
|
96
104
|
|
|
105
|
+
// currently not processing batch messages
|
|
106
|
+
const records = context.invocation?.event?.Records;
|
|
107
|
+
|
|
108
|
+
if (!isNonEmptyArray(records)) {
|
|
109
|
+
log.warn('Function was not invoked properly, event does not contain any records');
|
|
110
|
+
return badRequest('Event does not contain any records');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const record = records[0];
|
|
114
|
+
|
|
115
|
+
log.info(`Received ${records.length} records. ID of the first message in the batch: ${record.messageId}`);
|
|
116
|
+
|
|
97
117
|
try {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (!Array.isArray(records) || records.length === 0) {
|
|
101
|
-
throw new Error('No records found');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
log.info(`Received ${records.length} records. ID of the first message in the batch: ${records[0]?.messageId}`);
|
|
105
|
-
message = JSON.parse(records[0]?.body);
|
|
106
|
-
log.info(`Received message with id: ${records[0]?.messageId}`);
|
|
118
|
+
message = JSON.parse(record.body);
|
|
119
|
+
log.info(`Received message with id: ${record.messageId}`);
|
|
107
120
|
} catch (e) {
|
|
108
121
|
log.warn('Function was not invoked properly, message body is not a valid JSON', e);
|
|
109
|
-
return
|
|
110
|
-
status: 400,
|
|
111
|
-
headers: {
|
|
112
|
-
'x-error': 'Event does not contain a valid message body',
|
|
113
|
-
},
|
|
114
|
-
});
|
|
122
|
+
return badRequest('Event does not contain a valid message body');
|
|
115
123
|
}
|
|
116
124
|
return fn(message, context);
|
|
117
125
|
};
|