Skip to content

Commit 5d8bd67

Browse files
committed
style(batch): address SonarCloud issues
1 parent 077c75f commit 5d8bd67

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

packages/batch/tests/helpers/handlers.ts

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
AttributeValue,
23
Context,
34
DynamoDBRecord,
45
KinesisStreamRecord,
@@ -8,60 +9,90 @@ import type {
89
const sqsRecordHandler = (record: SQSRecord): string => {
910
const body = record.body;
1011
if (body.includes('fail')) {
11-
throw Error('Failed to process record.');
12+
throw new Error('Failed to process record.');
1213
}
1314

1415
return body;
1516
};
1617

17-
const asyncSqsRecordHandler = async (record: SQSRecord): Promise<string> =>
18-
Promise.resolve(sqsRecordHandler(record));
18+
const asyncSqsRecordHandler = async (record: SQSRecord): Promise<string> => {
19+
const body = record.body;
20+
if (body.includes('fail')) {
21+
throw new Error('Failed to process record.');
22+
}
23+
await new Promise((resolve) => setTimeout(resolve, 1));
24+
return body;
25+
};
1926

2027
const kinesisRecordHandler = (record: KinesisStreamRecord): string => {
2128
const body = record.kinesis.data;
2229
if (body.includes('fail')) {
23-
throw Error('Failed to process record.');
30+
throw new Error('Failed to process record.');
2431
}
2532

2633
return body;
2734
};
2835

2936
const asyncKinesisRecordHandler = async (
3037
record: KinesisStreamRecord
31-
): Promise<string> => Promise.resolve(kinesisRecordHandler(record));
38+
): Promise<string> => {
39+
const body = record.kinesis.data;
40+
if (body.includes('fail')) {
41+
throw new Error('Failed to process record.');
42+
}
43+
await new Promise((resolve) => setTimeout(resolve, 1));
44+
return body;
45+
};
3246

33-
const dynamodbRecordHandler = (record: DynamoDBRecord): object => {
47+
const dynamodbRecordHandler = (record: DynamoDBRecord): AttributeValue => {
3448
const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
3549
if (body.S?.includes('fail')) {
36-
throw Error('Failed to process record.');
50+
throw new Error('Failed to process record.');
3751
}
3852

3953
return body;
4054
};
4155

42-
const asyncDynamodbRecordHandler = (
56+
const asyncDynamodbRecordHandler = async (
4357
record: DynamoDBRecord
44-
): Promise<object> => {
45-
return Promise.resolve(dynamodbRecordHandler(record));
58+
): Promise<AttributeValue> => {
59+
const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
60+
if (body.S?.includes('fail')) {
61+
throw new Error('Failed to process record.');
62+
}
63+
await new Promise((resolve) => setTimeout(resolve, 1));
64+
return body;
4665
};
4766

4867
const handlerWithContext = (record: SQSRecord, context: Context): string => {
4968
try {
5069
if (context.getRemainingTimeInMillis() === 0) {
51-
throw Error('No time remaining.');
70+
throw new Error('No time remaining.');
5271
}
5372
} catch {
54-
throw Error(`Context possibly malformed. Displaying context:\n${context}`);
73+
throw new Error(
74+
`Context possibly malformed. Displaying context:\n${context}`
75+
);
5576
}
5677

5778
return record.body;
5879
};
5980

60-
const asyncHandlerWithContext = (
81+
const asyncHandlerWithContext = async (
6182
record: SQSRecord,
6283
context: Context
6384
): Promise<string> => {
64-
return Promise.resolve(handlerWithContext(record, context));
85+
try {
86+
if (context.getRemainingTimeInMillis() === 0) {
87+
throw new Error('No time remaining.');
88+
}
89+
} catch {
90+
throw new Error(
91+
`Context possibly malformed. Displaying context:\n${context}`
92+
);
93+
}
94+
await new Promise((resolve) => setTimeout(resolve, 1));
95+
return record.body;
6596
};
6697

6798
export {

0 commit comments

Comments
 (0)