File tree Expand file tree Collapse file tree 3 files changed +30
-6
lines changed Expand file tree Collapse file tree 3 files changed +30
-6
lines changed Original file line number Diff line number Diff line change
1
+ import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env' ;
1
2
import type {
2
3
DynamoDBRecord ,
3
4
KinesisStreamRecord ,
4
5
SQSRecord ,
5
6
} from 'aws-lambda' ;
7
+ import type { GenericLogger } from '../../commons/lib/esm/types/GenericLogger.js' ;
6
8
import { BasePartialProcessor } from './BasePartialProcessor.js' ;
7
9
import {
8
10
DATA_CLASS_MAPPING ,
@@ -43,6 +45,13 @@ abstract class BasePartialBatchProcessor extends BasePartialProcessor {
43
45
*/
44
46
public eventType : keyof typeof EventType ;
45
47
48
+ /**
49
+ * A logger instance to be used for logging debug, warning, and error messages.
50
+ *
51
+ * When no logger is provided, we'll only log warnings and errors using the global `console` object.
52
+ */
53
+ protected readonly logger : Pick < GenericLogger , 'debug' | 'warn' | 'error' > ;
54
+
46
55
/**
47
56
* The configuration options for the parser integration
48
57
*/
@@ -66,6 +75,15 @@ abstract class BasePartialBatchProcessor extends BasePartialProcessor {
66
75
[ EventType . DynamoDBStreams ] : ( ) => this . collectDynamoDBFailures ( ) ,
67
76
} ;
68
77
this . parserConfig = parserConfig ;
78
+ const alcLogLevel = getStringFromEnv ( {
79
+ key : 'AWS_LAMBDA_LOG_LEVEL' ,
80
+ defaultValue : '' ,
81
+ } ) ;
82
+ this . logger = parserConfig ?. logger ?? {
83
+ debug : alcLogLevel === 'DEBUG' ? console . debug : ( ) => undefined ,
84
+ error : console . error ,
85
+ warn : console . warn ,
86
+ } ;
69
87
}
70
88
71
89
/**
Original file line number Diff line number Diff line change @@ -225,9 +225,11 @@ class BatchProcessor extends BasePartialBatchProcessor {
225
225
return result . data as EventSourceDataClassTypes ;
226
226
}
227
227
const issues = result . error . cause as ReadonlyArray < StandardSchemaV1 . Issue > ;
228
- throw new Error (
229
- `Failed to parse record: ${ issues . map ( ( issue ) => `${ issue ?. path ?. join ( '.' ) } : ${ issue . message } ` ) . join ( '; ' ) } `
230
- ) ;
228
+ const errorMessage = issues
229
+ . map ( ( issue ) => `${ issue ?. path ?. join ( '.' ) } : ${ issue . message } ` )
230
+ . join ( '; ' ) ;
231
+ this . logger . error ( errorMessage ) ;
232
+ throw new Error ( errorMessage ) ;
231
233
}
232
234
233
235
/**
@@ -256,7 +258,7 @@ class BatchProcessor extends BasePartialBatchProcessor {
256
258
if ( innerSchema != null ) {
257
259
// Only proceed with schema extension if it's a Zod schema
258
260
if ( innerSchema [ '~standard' ] . vendor !== SchemaVendor . Zod ) {
259
- console . warn (
261
+ this . logger . error (
260
262
'The schema provided is not supported. Only Zod schemas are supported for extension.'
261
263
) ;
262
264
throw new Error ( 'Unsupported schema type' ) ;
@@ -275,6 +277,9 @@ class BatchProcessor extends BasePartialBatchProcessor {
275
277
} ) ;
276
278
return this . #parseWithErrorHandling( record , schemaWithoutTransformers ) ;
277
279
}
280
+ this . logger . error (
281
+ 'The schema provided is not supported. Only Zod schemas are supported for extension.'
282
+ ) ;
278
283
throw new Error ( 'Either schema or innerSchema is required for parsing' ) ;
279
284
}
280
285
}
Original file line number Diff line number Diff line change @@ -94,8 +94,8 @@ type PartialItemFailureResponse = { batchItemFailures: PartialItemFailures[] };
94
94
/**
95
95
* Type representing the parser configuration options passed to the BasePartialBatchProcessor class.
96
96
*
97
- * @property schema - The schema to be used for parsing
98
- * @property innerSchema - The schema for the inner payload
97
+ * @property schema - The full event schema to be used for parsing
98
+ * @property innerSchema - The inner payload schema
99
99
* @property transformer - The transformer to be used for parsing the payload
100
100
* @property logger - The logger to be used for logging debug and warning messages.
101
101
*/
@@ -113,6 +113,7 @@ type BasePartialBatchProcessorParserConfig = {
113
113
innerSchema ?: StandardSchemaV1 ;
114
114
/**
115
115
* The transformer to be used for parsing the payload.
116
+ * No transformers will be used if this is not provided.
116
117
* Supported transformers are:
117
118
* 1. 'json': Uses JSONStringified helper
118
119
* 2. 'base64': Uses Base64Encoded helper
You can’t perform that action at this time.
0 commit comments