-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathextendingFailure.ts
46 lines (39 loc) · 1.22 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
import { Metrics, MetricUnit } from '@aws-lambda-powertools/metrics';
import {
BatchProcessor,
EventType,
processPartialResponse,
} from '@aws-lambda-powertools/batch';
import type {
FailureResponse,
EventSourceDataClassTypes,
} from '@aws-lambda-powertools/batch/types';
import { Logger } from '@aws-lambda-powertools/logger';
import type { SQSRecord, SQSHandler } 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: EventSourceDataClassTypes,
error: Error
): FailureResponse {
this.#metrics.addMetric('BatchRecordFailures', MetricUnit.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: SQSHandler = async (event, context) =>
processPartialResponse(event, recordHandler, processor, {
context,
});