VMError (Error Reporting)
VMError is a built-in structured error-reporting module that comes with LightVM to describe runtime failures, validation errors, and host-system problems with error codes and contextual diagnostics. The Rust VMError enum preserves details about each failure, while its formatter produces color-coded messages with configurable debugging information.
How VMError Works
VMError uses a Structured Error Enum (VMError) to represent failures and a Diagnostic Formatter (Display) to turn them into readable reports. The formatter reads thread-local configuration when available, otherwise uses the global configuration, and combines the error message with metadata, documentation links, optional internal backtraces, and hints. Formatting an error does not itself terminate the process; the caller determines how the failure is handled.
- Structured Rust Error Variants: Represents stack overflow and underflow, invalid opcodes, type mismatches, out-of-bounds access, invalid jump targets, restricted features, resource limits, unauthorized modules, invalid values, and system errors. Variant fields preserve relevant details such as stack limits, expected and actual types, collection lengths, and module names.
- Instruction Pointer Context: Includes the stored
ipand error type in reports for variants carrying instruction context.ExcessiveNopPadding,InvalidMaxTicksConfig, andTickLimitExceededhave no storedip, so the formatter displays0as a placeholder. RustSystemErrorreports omit instruction-pointer and error-type metadata. - Diagnostic Error Codes: Maps runtime and validation variants to
LVM001–LVM017andSystemErrortoLVM500througherror_code(), providing consistent identifiers for troubleshooting. - Configurable Hints & Explanations: Uses
VMErrorContainersettings (hint,explain,diagnostic_links, andbacktrace) to control diagnostic output. Hints and documentation links are enabled by default, while explanations and backtraces are disabled. When hints are enabled,explainselects the longer explanation instead of the short hint. - Error Documentation Links: Uses
diagnostic_link()to generate a documentation URL for each error code. Thediagnostic_linkssetting controls whether the formatter includes this URL, independently of hints. - Optional Internal Backtraces: When
backtraceis enabled, formats a previously captured thread-local stack snapshot with up to five matching LightVM symbols, excluding the backtrace helpers. If no snapshot or matching symbols are available, the report includes an unavailable-backtrace message. - Dying Color Constants: Reuses Dying's
REDandBOLDfor the error heading,CYANfor diagnostic branches,DARK_GRAYfor supporting details, andRESETto reset styling. These constants provide visual formatting without making VMError a process-termination mechanism. - TypeScript System Error Class: TypeScript's
VMSystemError, exported asVMError, extendsErrorand represents onlyLVM500system errors, rather than all Rust error variants. It storesdetail,hintDetails, and anipof0; itsprint(explain, hint)method writes a formatted report withconsole.errorand captures a JavaScript stack trace when supported.