@@ -208,6 +208,28 @@ class BatchProcessor extends BasePartialBatchProcessor {
208
208
}
209
209
}
210
210
211
+ /**
212
+ * Parse the record with the passed schema and
213
+ * return the result or throw the error depending on parsing success
214
+ *
215
+ * @param record - The record to be parsed
216
+ * @param schema - The modified schema to parse with
217
+ */
218
+ async #parseWithErrorHandling(
219
+ record : EventSourceDataClassTypes ,
220
+ schema : StandardSchemaV1
221
+ ) {
222
+ const { parse } = await import ( '@aws-lambda-powertools/parser' ) ;
223
+ const result = parse ( record , undefined , schema , true ) ;
224
+ if ( result . success ) {
225
+ return result . data as EventSourceDataClassTypes ;
226
+ }
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
+ ) ;
231
+ }
232
+
211
233
/**
212
234
* Parse the record according to the schema and event type passed.
213
235
*
@@ -226,19 +248,10 @@ class BatchProcessor extends BasePartialBatchProcessor {
226
248
if ( this . parserConfig == null ) {
227
249
return record ;
228
250
}
229
- const { parse } = await import ( '@aws-lambda-powertools/parser' ) ;
230
251
const { schema, innerSchema, transformer } = this . parserConfig ;
231
252
// If the external schema is specified, use it to parse the record
232
253
if ( schema != null ) {
233
- const extendedSchemaParsing = parse ( record , undefined , schema , true ) ;
234
- if ( extendedSchemaParsing . success ) {
235
- return extendedSchemaParsing . data as EventSourceDataClassTypes ;
236
- }
237
- const issues = extendedSchemaParsing . error
238
- . cause as ReadonlyArray < StandardSchemaV1 . Issue > ;
239
- throw new Error (
240
- `Failed to parse record: ${ issues . map ( ( issue ) => `${ issue ?. path ?. join ( '.' ) } : ${ issue . message } ` ) . join ( '; ' ) } `
241
- ) ;
254
+ return this . #parseWithErrorHandling( record , schema ) ;
242
255
}
243
256
if ( innerSchema != null ) {
244
257
// Only proceed with schema extension if it's a Zod schema
@@ -254,39 +267,13 @@ class BatchProcessor extends BasePartialBatchProcessor {
254
267
innerSchema,
255
268
transformer,
256
269
} ) ;
257
- const schemaWithTransformersParsing = parse (
258
- record ,
259
- undefined ,
260
- schemaWithTransformers ,
261
- true
262
- ) ;
263
- if ( schemaWithTransformersParsing . success ) {
264
- return schemaWithTransformersParsing . data as EventSourceDataClassTypes ;
265
- }
266
- const issues = schemaWithTransformersParsing . error
267
- . cause as ReadonlyArray < StandardSchemaV1 . Issue > ;
268
- throw new Error (
269
- `Failed to parse record: ${ issues . map ( ( issue ) => `${ issue ?. path ?. join ( '.' ) } : ${ issue . message } ` ) . join ( '; ' ) } `
270
- ) ;
270
+ return this . #parseWithErrorHandling( record , schemaWithTransformers ) ;
271
271
}
272
272
const schemaWithoutTransformers = await this . #createExtendedSchema( {
273
273
eventType,
274
274
innerSchema,
275
275
} ) ;
276
- const schemaWithoutTransformersParsing = parse (
277
- record ,
278
- undefined ,
279
- schemaWithoutTransformers ,
280
- true
281
- ) ;
282
- if ( schemaWithoutTransformersParsing . success ) {
283
- return schemaWithoutTransformersParsing . data as EventSourceDataClassTypes ;
284
- }
285
- const issues = schemaWithoutTransformersParsing . error
286
- . cause as ReadonlyArray < StandardSchemaV1 . Issue > ;
287
- throw new Error (
288
- `Failed to parse record: ${ issues . map ( ( issue ) => `${ issue ?. path ?. join ( '.' ) } : ${ issue . message } ` ) . join ( '; ' ) } `
289
- ) ;
276
+ return this . #parseWithErrorHandling( record , schemaWithoutTransformers ) ;
290
277
}
291
278
throw new Error ( 'Either schema or innerSchema is required for parsing' ) ;
292
279
}
0 commit comments