Skip to content

XMLMessages

do- edited this page Apr 21, 2026 · 10 revisions

XMLMessages is a static utility class for consistent error reporting across xml-toolkit.

It centralizes:

  • error code definitions (human-readable templates),
  • message formatting with argument substitution,
  • error object creation with debug metadata.

The standard util.format is used as a template system.

Usage

Localization

XMLMessages.VOCABULARY.set ('XVC-00005', `Il manque l'attribut obligatiore: "%s"`)

In a Validator

if (!node.attributes.has('id')) {
  XMLMessages.raise (['XVC-00005', 'id'])
}

Custom Error Handler

process.on ('uncaughtException', err => {
  if (err.args?.[0]?.startsWith('XVS-')) {
    // Handle simple type validation errors specially
    logger.warn('Data validation failed:', err.message)
  } else {
    throw err
  }
})

Static Methods

format(args)

Formats an error message by substituting arguments into the template.

Param Type Description
args Array [code, ...substitutions] — error code followed by values for %s, %i, %j etc.

Returns: string — formatted message with code prefix.

Example:

XMLMessages.format(['XVS-00004', 'abc', 3, 2])
// → 'XVS-00004 The value \'abc\' has the length 3, which exceeds the allowed maximum of 2'

raise(args)

Creates and throws an Error with formatted message and debug metadata.

Param Type Description
args Array Same as for format()

Throws: Error with properties:

Property Type Description
message string Formatted error text (code + description)
args Array Original arguments array (for debugging/logging)

Example:

try {
  XMLMessages.raise(['XVC-00005', 'id'])
} catch (e) {
  console.log(e.message)  // 'XVC-00005 Missing required attribute: "id"'
  console.log(e.args)     // ['XVC-00005', 'id']
}

Error Code Vocabulary

All messages are stored in XMLMessages.VOCABULARY — a Map<code, template>.

Prefixes

Prefix Scope Example codes
XML- Core XML parsing XML-00001, XML-00002, XML-00003
XSD- XML Schema definitions XSD-00001, XSD-00002
XVC- Validation: structure/constraints XVC-00001XVC-00005
XVS- Validation: simple type values XVS-00001XVS-00041

Selected Messages

XML Parsing (XML-*)

Code Template When raised
XML-00001 maxLength=%i exceeded XMLLexer encountered a lexeme longer than maxLength option
XML-00002 Unbalanced end element </tag> without matching <tag>
XML-00003 Unmatched end element, %s expected </wrong> when </expected> was anticipated

Schema Definitions (XSD-*)

Code Template When raised
XSD-00001 Unknown namespace: %s Referenced namespace not found in loaded schemas
XSD-00002 The element %s is not found in %s Element used but not declared in schema

Structural Validation (XVC-*)

Code Template When raised
XVC-00001 No nested elements allowed inside %s Complex type with simpleContent received child elements
XVC-00002 %s is unexpected here; should be %s Element order violates xs:sequence/xs:choice
XVC-00003 Unknown attribute: %s Attribute not declared in schema
XVC-00004 The attribute "%s" must have the value "%s", not "%s" Fixed-value attribute mismatch
XVC-00005 Missing required attribute: "%s" Required attribute absent

Simple Type Validation (XVS-*)

Code Template When raised
XVS-00001 The value "%s" doesn't match the pattern %s xs:pattern facet mismatch
XVS-00003 The value '%s' is not in list: %s xs:enumeration facet mismatch
XVS-00004006 Length constraint violations xs:minLength/maxLength/length
XVS-000070010 Range constraint violations xs:minInclusive/maxExclusive etc.
XVS-0001100012 Boolean parsing errors Invalid true/false/1/0
XVS-0001300019 Decimal parsing errors Invalid format, digit count, precision
XVS-0002000021 Float parsing errors Invalid floating-point syntax
XVS-0002200041 Date/time parsing errors Invalid date, time, timezone, separators

Clone this wiki locally