Skip to content

errorHandler

DDataParser.errorHandler(inner, transformers) wraps a parser and reassigns messages on the issues it produces. It lets you define user-facing errors after the schema has been declared.

Interactive example

Syntax

ts
DDataParser.errorHandler(inner, transformers, definition?)
DDataParserExtended.errorHandler(inner, transformers, definition?)
dataParser.errorHandler(transformers, definition?)

Parameters

  • inner: parser to execute before rewriting messages.
  • transformers: one transformer or an array of transformers. A transformer receives the issue source and returns a replacement message or null.
  • definition.checkers: applied to the final parsed value after message rewriting. Issues produced by these checkers are not rewritten by this error handler.
  • definition.errorMessage: generic message for issues produced by the error handler itself.

Return value

A DataParserErrorHandler. schema.parse(data) returns the same success or error state as inner, but every produced issue can receive a new message based on its source parser or checker.

Checkers added directly to the returned DataParserErrorHandler run after the handler has rewritten the inner parser issues. If those checkers fail, their messages keep their own errorMessage or the handler definition.errorMessage.

Other examples

Extended mode

See also

  • recover - Return a fallback value when a parser fails
  • refine - Add custom validation rules with messages

Released under the MIT license.