-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathextendingFailure.ts
53 lines (46 loc) · 1.23 KB
/
extendingFailure.ts
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
51
52
53
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
import {
BatchProcessor,
EventType,
FailureResponse,
EventSourceType,
processPartialResponse,
} from '@aws-lambda-powertools/batch';
import { Logger } from '@aws-lambda-powertools/logger';
import type {
SQSEvent,
SQSRecord,
Context,
SQSBatchResponse,
} from 'aws-lambda';
class MyProcessor extends BatchProcessor {
#metrics: Metrics;
public constructor(eventType: keyof typeof EventType) {
super(eventType);
this.#metrics = new Metrics({ namespace: 'test' });
}
public failureHandler(
record: EventSourceType,
error: Error
): FailureResponse {
this.#metrics.addMetric('BatchRecordFailures', MetricUnits.Count, 1);
return super.failureHandler(record, error);
}
}
const processor = new MyProcessor(EventType.SQS);
const logger = new Logger();
const recordHandler = (record: SQSRecord): void => {
const payload = record.body;
if (payload) {
const item = JSON.parse(payload);
logger.info('Processed item', { item });
}
};
export const handler = async (
event: SQSEvent,
context: Context
): Promise<SQSBatchResponse> => {
return processPartialResponse(event, recordHandler, processor, {
context,
});
};