1
0

Refine error output

This commit is contained in:
Jeff 2024-03-18 05:47:21 -04:00
parent 7dcfccf7cb
commit 18b8fd6681

View File

@ -30,7 +30,7 @@ pub enum Error {
impl Error { impl Error {
pub fn build_report<'a>(self) -> ReportBuilder<'a, (&'a str, Range<usize>)> { pub fn build_report<'a>(self) -> ReportBuilder<'a, (&'a str, Range<usize>)> {
let (mut builder, validation_error) = match &self { let (mut builder, validation_error, error_position) = match self {
Error::Parse { expected, span } => { Error::Parse { expected, span } => {
let message = if expected.is_empty() { let message = if expected.is_empty() {
"Invalid token.".to_string() "Invalid token.".to_string()
@ -50,6 +50,7 @@ impl Error {
.with_color(Color::Red), .with_color(Color::Red),
), ),
None, None,
span.into(),
) )
} }
Error::Lex { expected, span } => { Error::Lex { expected, span } => {
@ -71,6 +72,7 @@ impl Error {
.with_color(Color::Red), .with_color(Color::Red),
), ),
None, None,
span.into(),
) )
} }
Error::Runtime { error, position } => ( Error::Runtime { error, position } => (
@ -84,6 +86,7 @@ impl Error {
} else { } else {
None None
}, },
position,
), ),
Error::Validation { error, position } => ( Error::Validation { error, position } => (
Report::build( Report::build(
@ -92,6 +95,7 @@ impl Error {
position.1, position.1,
), ),
Some(error), Some(error),
position,
), ),
}; };
@ -134,10 +138,14 @@ impl Error {
.with_message(format!("Got type {} here.", actual.fg(type_color))), .with_message(format!("Got type {} here.", actual.fg(type_color))),
]); ]);
} }
ValidationError::VariableNotFound(identifier) => builder.set_message(format!( ValidationError::VariableNotFound(identifier) => builder.add_label(
"{} does not exist in this context.", Label::new(("input", error_position.0..error_position.1)).with_message(
format!(
"Variable {} does not exist in this context.",
identifier.fg(identifier_color) identifier.fg(identifier_color)
)), ),
),
),
ValidationError::CannotIndex { r#type, position } => builder.add_label( ValidationError::CannotIndex { r#type, position } => builder.add_label(
Label::new(("input", position.0..position.1)) Label::new(("input", position.0..position.1))
.with_message(format!("Cannot index into a {}.", r#type.fg(type_color))), .with_message(format!("Cannot index into a {}.", r#type.fg(type_color))),