1+ import { setTimeout } from 'node:timers/promises' ;
12import type {
3+ AttributeValue ,
24 Context ,
35 DynamoDBRecord ,
46 KinesisStreamRecord ,
@@ -8,50 +10,70 @@ import type {
810const sqsRecordHandler = ( record : SQSRecord ) : string => {
911 const body = record . body ;
1012 if ( body . includes ( 'fail' ) ) {
11- throw Error ( 'Failed to process record.' ) ;
13+ throw new Error ( 'Failed to process record.' ) ;
1214 }
1315
1416 return body ;
1517} ;
1618
17- const asyncSqsRecordHandler = async ( record : SQSRecord ) : Promise < string > =>
18- Promise . resolve ( sqsRecordHandler ( record ) ) ;
19+ const asyncSqsRecordHandler = async ( record : SQSRecord ) : Promise < string > => {
20+ const body = record . body ;
21+ if ( body . includes ( 'fail' ) ) {
22+ throw new Error ( 'Failed to process record.' ) ;
23+ }
24+ await setTimeout ( 1 ) ; // simulate some processing time
25+ return body ;
26+ } ;
1927
2028const kinesisRecordHandler = ( record : KinesisStreamRecord ) : string => {
2129 const body = record . kinesis . data ;
2230 if ( body . includes ( 'fail' ) ) {
23- throw Error ( 'Failed to process record.' ) ;
31+ throw new Error ( 'Failed to process record.' ) ;
2432 }
2533
2634 return body ;
2735} ;
2836
2937const asyncKinesisRecordHandler = async (
3038 record : KinesisStreamRecord
31- ) : Promise < string > => Promise . resolve ( kinesisRecordHandler ( record ) ) ;
39+ ) : Promise < string > => {
40+ const body = record . kinesis . data ;
41+ if ( body . includes ( 'fail' ) ) {
42+ throw new Error ( 'Failed to process record.' ) ;
43+ }
44+ await setTimeout ( 1 ) ; // simulate some processing time
45+ return body ;
46+ } ;
3247
33- const dynamodbRecordHandler = ( record : DynamoDBRecord ) : object => {
48+ const dynamodbRecordHandler = ( record : DynamoDBRecord ) : AttributeValue => {
3449 const body = record . dynamodb ?. NewImage ?. Message || { S : 'fail' } ;
3550 if ( body . S ?. includes ( 'fail' ) ) {
36- throw Error ( 'Failed to process record.' ) ;
51+ throw new Error ( 'Failed to process record.' ) ;
3752 }
3853
3954 return body ;
4055} ;
4156
4257const asyncDynamodbRecordHandler = async (
4358 record : DynamoDBRecord
44- ) : Promise < object > => {
45- return Promise . resolve ( dynamodbRecordHandler ( record ) ) ;
59+ ) : Promise < AttributeValue > => {
60+ const body = record . dynamodb ?. NewImage ?. Message || { S : 'fail' } ;
61+ if ( body . S ?. includes ( 'fail' ) ) {
62+ throw new Error ( 'Failed to process record.' ) ;
63+ }
64+ await setTimeout ( 1 ) ; // simulate some processing time
65+ return body ;
4666} ;
4767
4868const handlerWithContext = ( record : SQSRecord , context : Context ) : string => {
4969 try {
5070 if ( context . getRemainingTimeInMillis ( ) === 0 ) {
51- throw Error ( 'No time remaining.' ) ;
71+ throw new Error ( 'No time remaining.' ) ;
5272 }
5373 } catch {
54- throw Error ( `Context possibly malformed. Displaying context:\n${ context } ` ) ;
74+ throw new Error (
75+ `Context possibly malformed. Displaying context:\n${ context } `
76+ ) ;
5577 }
5678
5779 return record . body ;
@@ -61,7 +83,17 @@ const asyncHandlerWithContext = async (
6183 record : SQSRecord ,
6284 context : Context
6385) : Promise < string > => {
64- return Promise . resolve ( handlerWithContext ( record , context ) ) ;
86+ try {
87+ if ( context . getRemainingTimeInMillis ( ) === 0 ) {
88+ throw new Error ( 'No time remaining.' ) ;
89+ }
90+ } catch {
91+ throw new Error (
92+ `Context possibly malformed. Displaying context:\n${ context } `
93+ ) ;
94+ }
95+ await setTimeout ( 1 ) ; // simulate some processing time
96+ return record . body ;
6597} ;
6698
6799export {
0 commit comments