Error:
{\"errorType\":\"TypeError\",\"errorMessage\":\"Cannot read properties of undefined (reading 'length')\",
My code:
export function throwZodError(message: string) {
getLogger().error('throwZodError', {
error: new ZodError([
<ZodIssue>{
message,
},
]),
});
throw new ZodError([<ZodIssue>{ message }]);
}
the logs:
"error": {
"name": "ZodError",
"location": "/packages/schemas/src/zod/index.ts:20",
"message": "[\n {\n \"message\": \"Schema validation error(s): body REDACTED",
"stack": "ZodError: [\n {\n \"message\": \"Schema validation error(s): body REDACTED]\"\n }\n]\n at throwZodError (/packages/schemas/src/zod/index.ts:20:12)\n at maybeThrowValidationError (/packages/redacted/util.ts:14:5)\n at Object._or (/packages/redacted/redacted.ts:81:5)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async R2n (/var/src/redacted.ts:38:22)\n at async <anonymous> (/var/src/redacted.ts:74:14)\n at async kTe (/node_modules/redacted/src/track-event.ts:61:20)\n at async TLt (/var/src/redacted.ts:68:10)\n at async FLt (/var/src/handler.ts:79:10)\n at async runRequest (/opt/nodejs/node_modules/@middy/core/index.cjs:130:32)",
"issues": [
{
"message": "Schema validation error(s): body REDACTED"
}
]
}
the stack trace:
at isNonEmptyArray (/opt/nodejs/node_modules/zod-validation-error/lib/utils/NonEmptyArray.ts:4:16)\",\"
at getMessageFromZodIssue (/opt/nodejs/node_modules/zod-validation-error/lib/MessageBuilder.ts:115:22)\",\"
at <anonymous> (/opt/nodejs/node_modules/zod-validation-error/lib/MessageBuilder.ts:42:9)\",\"
at Array.map (<anonymous>)\",\"
at <anonymous> (/opt/nodejs/node_modules/zod-validation-error/lib/MessageBuilder.ts:41:8)\",\"
at fromZodErrorWithoutRuntimeCheck (/opt/nodejs/node_modules/zod-validation-error/lib/fromZodError.ts:45:15)\",\"
at fromZodError (/opt/nodejs/node_modules/zod-validation-error/lib/fromZodError.ts:33:10)\",\"
Looks to me like it's hitting
// lib/utils/NonEmptyArray.ts
function isNonEmptyArray(value) {
return value.length !== 0;
}
and not checking if value is present before checking the length.
My other code that calls this looks like:
if (request.error instanceof z.ZodError) {
// we might have a Zod validation error here, so return a formatted validation message
error = new RedactedError(
`${fromZodError(request.error as ZodError)}`
);
}
is that incorrect usage?
Error:
My code:
the logs:
the stack trace:
Looks to me like it's hitting
and not checking if value is present before checking the length.
My other code that calls this looks like:
is that incorrect usage?