This module provides custom error classes for handling parsing failures and AST-related errors.
The ParseError class is thrown when the parser encounters invalid CSS syntax or fails to parse the input string.
Type: String
Value: 'ParseError'
Type: String
The error message describing what went wrong during parsing.
Type: String
The error stack trace from the underlying parsing error.
const { parse } = require('postcss-values-parser');
try {
const root = parse('invalid css syntax @#$%');
} catch (error) {
if (error instanceof ParseError) {
console.log(error.name); // 'ParseError'
console.log(error.message); // Description of the parsing error
}
}The AstError class is thrown when the AST (Abstract Syntax Tree) is invalid or empty after parsing.
Type: String
Value: 'AstError'
Type: String
Value: 'Invalid or empty AST'
const { parse } = require('postcss-values-parser');
try {
const root = parse('');
} catch (error) {
if (error instanceof AstError) {
console.log(error.name); // 'AstError'
console.log(error.message); // 'Invalid or empty AST'
}
}When using postcss-values-parser, it's recommended to handle both error types:
const { parse, ParseError, AstError } = require('postcss-values-parser');
function safeParseValue(css) {
try {
return parse(css);
} catch (error) {
if (error instanceof ParseError) {
console.error('Failed to parse CSS value:', error.message);
// Handle parsing syntax errors
} else if (error instanceof AstError) {
console.error('Invalid AST generated:', error.message);
// Handle empty or invalid AST
} else {
console.error('Unexpected error:', error);
// Handle other unexpected errors
}
return null;
}
}- Both error classes extend the standard JavaScript
Errorclass ParseErrorwraps errors from the underlying CSS parserAstErrorindicates issues with the generated AST structure- Error messages provide context about what went wrong during parsing
- Stack traces are preserved for debugging purposes