Skip to content

Commit fd5e899

Browse files
committed
Refactored the code to remove duplicates
1 parent 0160de4 commit fd5e899

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

packages/batch/src/BatchProcessor.ts

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,28 @@ class BatchProcessor extends BasePartialBatchProcessor {
208208
}
209209
}
210210

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+
211233
/**
212234
* Parse the record according to the schema and event type passed.
213235
*
@@ -226,19 +248,10 @@ class BatchProcessor extends BasePartialBatchProcessor {
226248
if (this.parserConfig == null) {
227249
return record;
228250
}
229-
const { parse } = await import('@aws-lambda-powertools/parser');
230251
const { schema, innerSchema, transformer } = this.parserConfig;
231252
// If the external schema is specified, use it to parse the record
232253
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);
242255
}
243256
if (innerSchema != null) {
244257
// Only proceed with schema extension if it's a Zod schema
@@ -254,39 +267,13 @@ class BatchProcessor extends BasePartialBatchProcessor {
254267
innerSchema,
255268
transformer,
256269
});
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);
271271
}
272272
const schemaWithoutTransformers = await this.#createExtendedSchema({
273273
eventType,
274274
innerSchema,
275275
});
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);
290277
}
291278
throw new Error('Either schema or innerSchema is required for parsing');
292279
}

0 commit comments

Comments
 (0)