Skip to content

Error with empty array #459

Description

@jcollum-nutrien

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions