{"record":{"id":"0aa0664eed08f585","repo":"colinhacks/zod","slug":"not-a-zoderror-value","errorCode":null,"errorMessage":"Not a ZodError: ${value}","messagePattern":"Not a ZodError: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v3/ZodError.ts","lineNumber":278,"sourceCode":"            }\n            i++;\n          }\n        }\n      }\n    };\n\n    processError(this);\n    return fieldErrors;\n  }\n\n  static create = (issues: ZodIssue[]) => {\n    const error = new ZodError(issues);\n    return error;\n  };\n\n  static assert(value: unknown): asserts value is ZodError {\n    if (!(value instanceof ZodError)) {\n      throw new Error(`Not a ZodError: ${value}`);\n    }\n  }\n\n  override toString() {\n    return this.message;\n  }\n  override get message() {\n    return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);\n  }\n\n  get isEmpty(): boolean {\n    return this.issues.length === 0;\n  }\n\n  addIssue = (sub: ZodIssue) => {\n    this.issues = [...this.issues, sub];\n  };\n","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/colinhacks/zod/blob/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v3/ZodError.ts#L260-L296","documentation":"Thrown by the static guard ZodError.assert(value) at packages/zod/src/v3/ZodError.ts:278 when the value passed in is not an instance of ZodError. The method is a runtime narrowing helper (signature: asserts value is ZodError), so calling it on anything else — a plain Error, a rejected promise reason, a custom error object — fails the assertion and throws a generic Error. It exists to let callers verify that a caught value really carries Zod issue data before reading .issues off it.","triggerScenarios":"Calling ZodError.assert(someValue) where someValue is a generic Error, a string, undefined, or any non-ZodError thrown from code unrelated to schema parsing. Common in catch blocks that indiscriminately pass the caught value to assert() without first checking instanceof.","commonSituations":"Mixing Zod with other validation libraries where a different library's error propagates into the same catch block; wrapping third-party code that throws non-Zod errors; refactoring that changes which errors a function can throw; using assert() on error causes from network/IO layers that wrap failures in their own Error subclasses.","solutions":["Guard with `value instanceof ZodError` before calling ZodError.assert(value), and handle the non-Zod branch separately.","In catch blocks, branch on the error type: `if (e instanceof ZodError) { ... } else { throw e; }` instead of unconditionally asserting.","Trace where the non-ZodError value originates and ensure that code path only throws ZodErrors (e.g. it parses through a schema) or rethrows other errors.","If you only need the issues list, use safeParse()/safeParseAsync() so failures never throw in the first place."],"exampleFix":"// before\ntry {\n  schema.parse(data);\n} catch (e) {\n  ZodError.assert(e);\n  console.log(e.issues);\n}\n\n// after\ntry {\n  schema.parse(data);\n} catch (e) {\n  if (e instanceof ZodError) {\n    console.log(e.issues);\n  } else {\n    throw e;\n  }\n}","handlingStrategy":"type-guard","validationCode":"null","typeGuard":"import { ZodError } from 'zod';\n\nfunction isZodError(value: unknown): value is ZodError {\n  return value instanceof ZodError;\n}\n\n// usage:\n// try { schema.parse(x); } catch (e) {\n//   if (isZodError(e)) handleIssues(e.issues);\n//   else throw e;\n// }","tryCatchPattern":"try {\n  schema.parse(data);\n} catch (e) {\n  if (e instanceof ZodError) {\n    // structured issue data is safe to read here\n    reportIssues(e.issues);\n  } else {\n    throw e; // never swallow unknown errors\n  }\n}","preventionTips":["Never call ZodError.assert() on a value you haven't already narrowed with instanceof ZodError.","Prefer safeParse()/safeParseAsync() so parse failures return a result instead of throwing.","Always branch on error type in catch blocks before reading Zod-specific properties.","Keep third-party error types in their own catch handlers, separate from Zod parse catch blocks."],"tags":["zoderror","assertion","runtime","instanceof"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}