Skip to main content

Crate context_error

Crate context_error 

Source
Expand description

Contain the definition for errors with all additional data that is needed to generate nice error messages.

ⓘ
use context_error::{CustomError, BasicKind, CreateError, Context};
let line = "null,80o0,YES,,67.77";
let split = line.split(',').collect::<Vec<&str>>();
let num: usize = split[1].parse().map_err(|err| CustomError::new(
    BasicKind::Error,
    "Invalid number",
    "This column is not a number",
    Context::default().lines(0, line).add_highlight((0, 5..9))))?;

Produces the following error:

error: Invalid number
 ╷
 │ null,80o0,YES,,67.77
 ╎      ╶──╴
 ╵
This column is not a number
  • The properties for an error can be inspected using StaticErrorContent and FullErrorContent.
  • Errors can be made using CreateError.
  • Two error types are already given: CustomError and BoxedError, the latter being a boxed version to prevent a lot stack space consumed by the result type in the happy case.
  • Errors can be combined for a more concise error report using combine_error and combine_errors.
  • Different ErrorKinds can be defined to enumerate all possible failure cases for easy matching.
  • The Context for an error can contain a lot of additional details to help highlight exactly where the error occurred.

Structs§

BoxedError
An error. Stored as a pointer to a structure on the heap to prevent large sizes which could be detrimental to performance for the happy path.
CombineErrors
An iterator adapter that keeps track separately of the errors to merge ones that can be merged. The errors have to be retrieved separately using Self::errors.
Context
A context construct to indicate a context presumably in a file, but could be in any kind of source text.
CustomError
FilePosition
A position in a file for use in parsing/lexing
Highlight
A highlight on a single line. The easiest way of creating these is by using the From implementations.

Enums§

BasicKind

Traits§

CombineErrorsExtender
An iterator adapter that keeps track separately of the errors to merge ones that can be merged. The errors have to be retrieved separately using CombineErrors::errors.
CreateError
A trait to guarantee identical an API between the boxed and unboxed error version
ErrorKind
The kind of an error
FullErrorContent
A structure that contains all error content
StaticErrorContent
A structure that contains basic error content

Functions§

combine_error
Combine a new error into a stack of existing errors. This merges errors that can be merged to be able to show a terser error if the same error happened multiple times in the same file.
combine_errors
Combine a list full of error into the list of already existing errors.