Overview
TheErrorFormatter class transforms ParseErrorBundle objects into human-readable error messages. It supports multiple output formats including plain text, ANSI-colored terminal output, HTML, and JSON.
ErrorFormatter Class
Supported Formats
- plain: Plain text without colors (default)
- ansi: Terminal output with ANSI color codes
- html: HTML with semantic classes for styling
- json: Structured JSON for programmatic consumption
Formatter Options
Basic Usage
Creating a Formatter
Formatting Errors
Output Formats
Plain Text Format
Simple text output without colors, suitable for logs or non-terminal environments.ANSI Format
Terminal output with colors using ANSI escape codes.- Red error messages and carets
- Yellow “Expected:” labels
- Cyan hints
- Gray context information
HTML Format
HTML output with semantic classes for custom styling..parse-error: Container for the entire error.error-header: Location information.error-context: Code context section.context-line: Individual context lines.error-line: The line with the error.error-pointer: Caret/pointer line.error-message: The error message.error-hints: Container for hints.hint: Individual hint.suggestion: Suggested correction.error-context-stack: Parser context stack.context-item: Individual context item
JSON Format
Structured JSON output for programmatic error handling.Customizing Output
Modifying Options
Create a new formatter with different options:Changing Format
Create a new formatter with a different output format:Context Lines
Control how many lines of source code are shown around the error:maxContextLines: 1- Only the error linemaxContextLines: 3- Error line + 1 above/below (default)maxContextLines: 5- Error line + 2 above/below
Convenience Functions
Quick formatting without creating a formatter instance:Integration with ParseErrorBundle
TheParseErrorBundle class has built-in formatting support:
Best Practices
- Use ANSI format for CLI tools and terminal output
- Use plain format for log files and non-terminal environments
- Use HTML format for web-based editors or documentation
- Use JSON format for programmatic error handling or IDE integration
- Show context to help users locate errors quickly
- Enable hints to improve the user experience for typos
- Adjust maxContextLines based on your use case (more for complex files, less for simple parsers)