Skip to content

Commit 75e233f

Browse files
authored
refactor(batch): improve code quality in test handlers (#4281)
Co-authored-by: David <75678655+David-Werth@users.noreply.github.com>
1 parent 01863bf commit 75e233f

File tree

1 file changed

+5
-31
lines changed

1 file changed

+5
-31
lines changed

packages/batch/tests/helpers/handlers.ts

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@ const sqsRecordHandler = (record: SQSRecord): string => {
1414
return body;
1515
};
1616

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

2620
const kinesisRecordHandler = (record: KinesisStreamRecord): string => {
2721
const body = record.kinesis.data;
@@ -34,14 +28,7 @@ const kinesisRecordHandler = (record: KinesisStreamRecord): string => {
3428

3529
const asyncKinesisRecordHandler = async (
3630
record: KinesisStreamRecord
37-
): Promise<string> => {
38-
const body = record.kinesis.data;
39-
if (body.includes('fail')) {
40-
throw Error('Failed to process record.');
41-
}
42-
43-
return body;
44-
};
31+
): Promise<string> => Promise.resolve(kinesisRecordHandler(record));
4532

4633
const dynamodbRecordHandler = (record: DynamoDBRecord): object => {
4734
const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
@@ -55,12 +42,7 @@ const dynamodbRecordHandler = (record: DynamoDBRecord): object => {
5542
const asyncDynamodbRecordHandler = async (
5643
record: DynamoDBRecord
5744
): Promise<object> => {
58-
const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
59-
if (body.S?.includes('fail')) {
60-
throw Error('Failed to process record.');
61-
}
62-
63-
return body;
45+
return Promise.resolve(dynamodbRecordHandler(record));
6446
};
6547

6648
const handlerWithContext = (record: SQSRecord, context: Context): string => {
@@ -79,15 +61,7 @@ const asyncHandlerWithContext = async (
7961
record: SQSRecord,
8062
context: Context
8163
): Promise<string> => {
82-
try {
83-
if (context.getRemainingTimeInMillis() === 0) {
84-
throw Error('No time remaining.');
85-
}
86-
} catch {
87-
throw Error(`Context possibly malformed. Displaying context:\n${context}`);
88-
}
89-
90-
return Promise.resolve(record.body);
64+
return Promise.resolve(handlerWithContext(record, context));
9165
};
9266

9367
export {

0 commit comments

Comments
 (0)