Expand description
§Error Handling in mon-core
This module defines the error types used throughout the mon-core library. The error
handling strategy is built around the miette crate for rich, diagnostic-style
error reporting, and thiserror for ergonomic error type definitions.
§Architectural Overview
The error system is hierarchical:
-
MonError: This is the top-level, public-facing error enum. It wraps more specific error types from different stages of the compilation pipeline. Any function in the public API will typically return aResult<T, MonError>. -
Phase-Specific Errors:
ParserError: Errors that occur during the lexing or parsing phase, such as syntax errors (e.g., an unexpected token).ResolverError: Errors that occur during the semantic analysis phase. This includes failures like an import not being found, an anchor being undefined, or a circular dependency.
-
ValidationError: A specialized subset of resolver errors that occur specifically during type validation. This includes type mismatches, missing or extra fields in structs, and undefined enum variants.
§Use Cases
When you use the mon-core library, you will primarily interact with MonError. You can
match on its variants (Parser or Resolver) to determine the source of the failure.
The rich diagnostic information provided by miette allows for printing user-friendly,
colorful error reports that point directly to the problematic code in the source file.
§Example: Handling an Error
use mon_core::api::analyze;
// This source code has a syntax error (a missing comma).
let source = "{ key1: \"value1\" key2: \"value2\" }";
let result = analyze(source, "bad.mon");
match result {
Ok(_) => println!("This should not have succeeded!"),
Err(err) => {
// You can render the beautiful, diagnostic error report.
// (Note: The actual output is a graphical report with colors and source snippets)
println!("{:?}", err);
// You can also programmatically inspect the error.
match err {
mon_core::error::MonError::Parser(p_err) => {
println!("A parser error occurred!");
},
_ => {}
}
}
}Enums§
- MonError
- The primary error type for the
mon-corelibrary. - Parser
Error - An error that occurred during the parsing phase.
- Resolver
Error - An error that occurred during the resolution or validation phase.
- Validation
Error - An error that occurred during data validation against a schema.