Update with-schema-parser.ts

This commit is contained in:
Catalin Pinte 2022-09-07 12:04:27 +03:00
parent 8ad6d26833
commit 163a22ec88

View File

@ -17,16 +17,25 @@ export const withSchemaParser =
return result return result
} catch (error) { } catch (error) {
if (error instanceof z.ZodError) { if (error instanceof z.ZodError) {
throw new CommerceError({ return Promise.reject(
code: 'SCHEMA_VALIDATION_ERROR', new CommerceError({
message: code: 'SCHEMA_VALIDATION_ERROR',
`The ${operation} opration returned invalid data: \n` + message:
error.issues `The ${operation} opration returned invalid data and has ${
.map((e) => `- ${e.path.join('.')}: ${e.message}`) error.issues.length
.join('\n'), } 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 { } else {
throw error return Promise.reject(error)
} }
} }
} }