From 163a22ec88d6ac495993ede7a130e16ca70bdd3a Mon Sep 17 00:00:00 2001 From: Catalin Pinte <1243434+cond0r@users.noreply.github.com> Date: Wed, 7 Sep 2022 12:04:27 +0300 Subject: [PATCH] Update with-schema-parser.ts --- .../src/api/utils/with-schema-parser.ts | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/commerce/src/api/utils/with-schema-parser.ts b/packages/commerce/src/api/utils/with-schema-parser.ts index 49b090e2f..f0875a171 100644 --- a/packages/commerce/src/api/utils/with-schema-parser.ts +++ b/packages/commerce/src/api/utils/with-schema-parser.ts @@ -17,16 +17,25 @@ export const withSchemaParser = return result } catch (error) { if (error instanceof z.ZodError) { - throw new CommerceError({ - code: 'SCHEMA_VALIDATION_ERROR', - message: - `The ${operation} opration returned invalid data: \n` + - error.issues - .map((e) => `- ${e.path.join('.')}: ${e.message}`) - .join('\n'), - }) + return Promise.reject( + new CommerceError({ + code: 'SCHEMA_VALIDATION_ERROR', + message: + `The ${operation} opration returned invalid data and has ${ + error.issues.length + } parse ${error.issues.length === 1 ? 'error' : 'errors'}: \n` + + error.issues + .map( + (e, index) => + `${index + 1}. Property ${e.path.join('.')} (${e.code}): ${ + e.message + }` + ) + .join('\n'), + }) + ) } else { - throw error + return Promise.reject(error) } } }