Skip to content

Error Handling

Returns a BuildResult, collects errors, and continues. Use when you want to handle failures gracefully (e.g. in a dev server or build pipeline that reports multiple issues).

Throws on the first error. Use when a single failure should stop the build (e.g. CI/CD or production builds).

type BuildResult = {
success: boolean
outputs: { name: string; path?: string; content: string }[]
errors?: BuildError[]
}
  • successtrue if no errors occurred
  • outputs — Array of generated outputs with name, path, and content
  • errors — Present when success is false; array of BuildError
type BuildError = {
message: string
code: ErrorCode
path?: string
tokenPath?: string
severity: 'error' | 'warning'
suggestions?: string[]
}
  • message — Human-readable error description
  • code — Error code for programmatic handling
  • path — File or token set path when relevant
  • tokenPath — Dot-path to the token (e.g. color.action.brand)
  • severityerror or warning
  • suggestions — Optional hints (e.g. similar token names for typos)
CodeDescription
TOKEN_REFERENCEUnresolved alias {token.name}
CIRCULAR_REFERENCECircular alias chain
VALIDATIONSchema or structural validation failure
FILE_OPERATIONFile read/write failure
CONFIGURATIONInvalid config
BASE_PERMUTATIONMissing base permutation for bundle mode
MODIFIERInvalid modifier input or context
UNKNOWNUnexpected error

TOKEN_REFERENCE errors often include suggestions with similar token names, helping you fix typos or wrong references.

Dispersa validates tokens and resolver documents against the official DTCG 2025.10 JSON schemas. Validation runs automatically during the Resolve and Parse pipeline stages and is configurable via validation.mode ('error' | 'warn' | 'off').

import { build, buildOrThrow } from 'dispersa'
const result = await build({ resolver: './tokens.resolver.json', outputs: [...] })
if (!result.success) {
for (const error of result.errors ?? []) {
console.error(`[${error.code}] ${error.message}`)
if (error.suggestions?.length) {
console.log(' Did you mean:', error.suggestions.join(', '))
}
}
}

When using buildOrThrow(), Dispersa throws typed error classes from dispersa/errors. You can catch specific error types for programmatic handling:

import { TokenReferenceError, CircularReferenceError } from 'dispersa/errors'
try {
await buildOrThrow({ resolver: './tokens.resolver.json', outputs: [...] })
} catch (error) {
if (error instanceof TokenReferenceError) {
console.log('Missing token:', error.referenceName)
console.log('Suggestions:', error.suggestions)
} else if (error instanceof CircularReferenceError) {
console.log('Circular chain:', error.referencePath)
}
}

Available error classes: DispersaError (base), TokenReferenceError, CircularReferenceError, ValidationError, FileOperationError, ConfigurationError, BasePermutationError, ModifierError.