langium-zod / ZodGeneratorError
Class: ZodGeneratorError
Defined in: errors.ts:70
Custom error class thrown by the langium-zod code generator when it encounters a condition it cannot recover from.
Carries optional structured context that pinpoints the source of the problem:
typeName— the Langium interface or union type being processed when the error occurred.grammarElement— the specific grammar property or rule element that triggered the failure.suggestion— a human-readable hint explaining how to fix the issue, surfaced to the user in CLI output.
The name property is always set to 'ZodGeneratorError' so that error handlers can distinguish this class from generic Error instances without needing an instanceof check across module boundaries.
Remarks
ZodGeneratorError is the only error class thrown by the public API. Standard Error (or subtypes) can still propagate from Langium internals (e.g. grammar parse failures), but those are not wrapped.
The name check (error.name === 'ZodGeneratorError') is safe across module boundaries where instanceof may fail when multiple copies of the package are bundled. Always prefer error.name over error instanceof ZodGeneratorError in plugin host environments.
Example
import { generateZodSchemas, ZodGeneratorError } from 'langium-zod';
try {
generateZodSchemas({ grammar: parsedGrammar });
} catch (err) {
if (err instanceof ZodGeneratorError) {
console.error(err.message);
if (err.suggestion) console.error('Hint:', err.suggestion);
if (err.typeName) console.error('Type:', err.typeName);
} else {
throw err;
}
}Use When
- You are wrapping generateZodSchemas in a try/catch and want to surface actionable error messages to the user.
- You are building a Vite/webpack plugin and need to map generation failures to build-time warnings.
Avoid When
- You do not need structured context — a plain
Error.messagecheck is sufficient for simple pipelines.
Never
- NEVER use
instanceof ZodGeneratorErrorin a plugin host that bundles its own copy of langium-zod. BECAUSEinstanceoffails across module boundaries when multiple instances of the class exist; useerror.name === 'ZodGeneratorError'instead.
See
Extends
Error
Constructors
Constructor
new ZodGeneratorError(message, options?): ZodGeneratorError;Defined in: errors.ts:75
Parameters
| Parameter | Type |
|---|---|
message | string |
options? | ZodGeneratorErrorOptions |
Returns
ZodGeneratorError
Overrides
Error.constructorProperties
| Property | Modifier | Type | Defined in |
|---|---|---|---|
grammarElement? | readonly | string | errors.ts:71 |
suggestion? | readonly | string | errors.ts:73 |
typeName? | readonly | string | errors.ts:72 |