Simplify error display.

This commit is contained in:
Stephen Chung 2021-07-02 11:50:24 +08:00
parent f3541bae19
commit 88bfe64e35
2 changed files with 86 additions and 72 deletions

View File

@ -98,8 +98,10 @@ impl Error for EvalAltResult {}
impl fmt::Display for EvalAltResult { impl fmt::Display for EvalAltResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Self::ErrorSystem(s, err) if s.is_empty() => write!(f, "{}", err)?, Self::ErrorSystem(s, err) => match s.as_str() {
Self::ErrorSystem(s, err) => write!(f, "{}: {}", s, err)?, "" => write!(f, "{}", err),
s => write!(f, "{}: {}", s, err),
}?,
Self::ErrorParsing(p, _) => write!(f, "Syntax error: {}", p)?, Self::ErrorParsing(p, _) => write!(f, "Syntax error: {}", p)?,
@ -128,76 +130,74 @@ impl fmt::Display for EvalAltResult {
Self::ErrorDataRace(s, _) => { Self::ErrorDataRace(s, _) => {
write!(f, "Data race detected when accessing variable: {}", s)? write!(f, "Data race detected when accessing variable: {}", s)?
} }
Self::ErrorDotExpr(s, _) if !s.is_empty() => f.write_str(s)?, Self::ErrorDotExpr(s, _) => match s.as_str() {
"" => f.write_str("Malformed dot expression"),
s => f.write_str(s),
}?,
Self::ErrorIndexingType(s, _) => write!(f, "Indexer not registered for '{}'", s)?, Self::ErrorIndexingType(s, _) => write!(f, "Indexer not registered for '{}'", s)?,
Self::ErrorUnboundThis(_) => f.write_str("'this' is not bound")?, Self::ErrorUnboundThis(_) => f.write_str("'this' is not bound")?,
Self::ErrorFor(_) => f.write_str("For loop expects a type with an iterator defined")?, Self::ErrorFor(_) => f.write_str("For loop expects a type with an iterator defined")?,
Self::ErrorDotExpr(_, _) => f.write_str("Malformed dot expression")?,
Self::ErrorTooManyOperations(_) => f.write_str("Too many operations")?, Self::ErrorTooManyOperations(_) => f.write_str("Too many operations")?,
Self::ErrorTooManyModules(_) => f.write_str("Too many modules imported")?, Self::ErrorTooManyModules(_) => f.write_str("Too many modules imported")?,
Self::ErrorStackOverflow(_) => f.write_str("Stack overflow")?, Self::ErrorStackOverflow(_) => f.write_str("Stack overflow")?,
Self::ErrorTerminated(_, _) => f.write_str("Script terminated")?, Self::ErrorTerminated(_, _) => f.write_str("Script terminated")?,
Self::ErrorRuntime(d, _) if d.is::<ImmutableString>() => {
let s = &*d
.read_lock::<ImmutableString>()
.expect("never fails because the type was checked");
if s.is_empty() {
f.write_str("Runtime error")?
} else {
write!(f, "Runtime error: {}", s)?
}
}
Self::ErrorRuntime(d, _) if d.is::<()>() => f.write_str("Runtime error")?, Self::ErrorRuntime(d, _) if d.is::<()>() => f.write_str("Runtime error")?,
Self::ErrorRuntime(d, _)
if d.read_lock::<ImmutableString>()
.map_or(false, |v| v.is_empty()) =>
{
write!(f, "Runtime error")?
}
Self::ErrorRuntime(d, _) => write!(f, "Runtime error: {}", d)?, Self::ErrorRuntime(d, _) => write!(f, "Runtime error: {}", d)?,
Self::ErrorAssignmentToConstant(s, _) => write!(f, "Cannot modify constant {}", s)?, Self::ErrorAssignmentToConstant(s, _) => write!(f, "Cannot modify constant {}", s)?,
Self::ErrorMismatchOutputType(s, r, _) => { Self::ErrorMismatchOutputType(s, r, _) => match (r.as_str(), s.as_str()) {
write!(f, "Output type is incorrect: {} (expecting {})", r, s)? ("", s) => write!(f, "Output type is incorrect, expecting {}", s),
} (r, "") => write!(f, "Output type is incorrect: {}", r),
Self::ErrorMismatchDataType(s, r, _) if r.is_empty() => { (r, s) => write!(f, "Output type is incorrect: {} (expecting {})", r, s),
write!(f, "Data type is incorrect, expecting {}", s)? }?,
} Self::ErrorMismatchDataType(s, r, _) => match (r.as_str(), s.as_str()) {
Self::ErrorMismatchDataType(s, r, _) if s.is_empty() => { ("", s) => write!(f, "Data type is incorrect, expecting {}", s),
write!(f, "Data type is incorrect: {}", r)? (r, "") => write!(f, "Data type is incorrect: {}", r),
} (r, s) => write!(f, "Data type is incorrect: {} (expecting {})", r, s),
Self::ErrorMismatchDataType(s, r, _) => { }?,
write!(f, "Data type is incorrect: {} (expecting {})", r, s)? Self::ErrorArithmetic(s, _) => match s.as_str() {
} "" => f.write_str("Arithmetic error"),
Self::ErrorArithmetic(s, _) => f.write_str(s)?, s => f.write_str(s),
}?,
Self::LoopBreak(true, _) => f.write_str("Break statement not inside a loop")?, Self::LoopBreak(true, _) => f.write_str("'break' not inside a loop")?,
Self::LoopBreak(false, _) => f.write_str("Continue statement not inside a loop")?, Self::LoopBreak(false, _) => f.write_str("'continue' not inside a loop")?,
Self::Return(_, _) => f.write_str("NOT AN ERROR - Function returns value")?, Self::Return(_, _) => f.write_str("NOT AN ERROR - function returns value")?,
Self::ErrorArrayBounds(0, index, _) => { Self::ErrorArrayBounds(max, index, _) => match max {
write!(f, "Array index {} out of bounds: array is empty", index)? 0 => write!(f, "Array index {} out of bounds: array is empty", index),
} 1 => write!(
Self::ErrorArrayBounds(1, index, _) => write!(
f, f,
"Array index {} out of bounds: only 1 element in the array", "Array index {} out of bounds: only 1 element in the array",
index index
)?, ),
Self::ErrorArrayBounds(max, index, _) => write!( _ => write!(
f, f,
"Array index {} out of bounds: only {} elements in the array", "Array index {} out of bounds: only {} elements in the array",
index, max index, max
)?, ),
Self::ErrorStringBounds(0, index, _) => { }?,
write!(f, "String index {} out of bounds: string is empty", index)? Self::ErrorStringBounds(max, index, _) => match max {
} 0 => write!(f, "String index {} out of bounds: string is empty", index),
Self::ErrorStringBounds(1, index, _) => write!( 1 => write!(
f, f,
"String index {} out of bounds: only 1 character in the string", "String index {} out of bounds: only 1 character in the string",
index index
)?, ),
Self::ErrorStringBounds(max, index, _) => write!( _ => write!(
f, f,
"String index {} out of bounds: only {} characters in the string", "String index {} out of bounds: only {} characters in the string",
index, max index, max
)?, ),
}?,
Self::ErrorBitFieldBounds(max, index, _) => write!( Self::ErrorBitFieldBounds(max, index, _) => write!(
f, f,
"Bit-field index {} out of bounds: only {} bits in the bit-field", "Bit-field index {} out of bounds: only {} bits in the bit-field",

View File

@ -194,12 +194,22 @@ impl fmt::Display for ParseErrorType {
Self::UnknownOperator(s) => write!(f, "Unknown operator: '{}'", s), Self::UnknownOperator(s) => write!(f, "Unknown operator: '{}'", s),
Self::MalformedCallExpr(s) if s.is_empty() => f.write_str("Invalid expression in function call arguments"), Self::MalformedCallExpr(s) => match s.as_str() {
Self::MalformedIndexExpr(s) if s.is_empty() => f.write_str("Invalid index in indexing expression"), "" => f.write_str("Invalid expression in function call arguments"),
Self::MalformedInExpr(s) if s.is_empty() => f.write_str("Invalid 'in' expression"), s => f.write_str(s)
Self::MalformedCapture(s) if s.is_empty() => f.write_str("Invalid capturing"), },
Self::MalformedIndexExpr(s) => match s.as_str() {
Self::MalformedCallExpr(s) | Self::MalformedIndexExpr(s) | Self::MalformedInExpr(s) | Self::MalformedCapture(s) => f.write_str(s), "" => f.write_str("Invalid index in indexing expression"),
s => f.write_str(s)
},
Self::MalformedInExpr(s) => match s.as_str() {
"" => f.write_str("Invalid 'in' expression"),
s => f.write_str(s)
},
Self::MalformedCapture(s) => match s.as_str() {
"" => f.write_str("Invalid capturing"),
s => f.write_str(s)
},
Self::FnDuplicatedDefinition(s, n) => { Self::FnDuplicatedDefinition(s, n) => {
write!(f, "Function '{}' with ", s)?; write!(f, "Function '{}' with ", s)?;
@ -209,12 +219,13 @@ impl fmt::Display for ParseErrorType {
_ => write!(f, "{} parameters already exists", n), _ => write!(f, "{} parameters already exists", n),
} }
} }
Self::FnMissingBody(s) => match s.as_str() {
Self::FnMissingBody(s) if s.is_empty() => f.write_str("Expecting body statement block for anonymous function"), "" => f.write_str("Expecting body statement block for anonymous function"),
Self::FnMissingBody(s) => write!(f, "Expecting body statement block for function '{}'", s), s => write!(f, "Expecting body statement block for function '{}'", s)
},
Self::FnMissingParams(s) => write!(f, "Expecting parameters for function '{}'", s), Self::FnMissingParams(s) => write!(f, "Expecting parameters for function '{}'", s),
Self::FnDuplicatedParam(s, arg) => write!(f, "Duplicated parameter '{}' for function '{}'", arg, s), Self::FnDuplicatedParam(s, arg) => write!(f, "Duplicated parameter '{}' for function '{}'", arg, s),
Self::DuplicatedProperty(s) => write!(f, "Duplicated property '{}' for object map literal", s), Self::DuplicatedProperty(s) => write!(f, "Duplicated property '{}' for object map literal", s),
Self::DuplicatedSwitchCase => f.write_str("Duplicated switch case"), Self::DuplicatedSwitchCase => f.write_str("Duplicated switch case"),
Self::DuplicatedVariable(s) => write!(f, "Duplicated variable name '{}'", s), Self::DuplicatedVariable(s) => write!(f, "Duplicated variable name '{}'", s),
@ -223,11 +234,14 @@ impl fmt::Display for ParseErrorType {
Self::MissingToken(token, s) => write!(f, "Expecting '{}' {}", token, s), Self::MissingToken(token, s) => write!(f, "Expecting '{}' {}", token, s),
Self::MissingSymbol(s) => f.write_str(s), Self::MissingSymbol(s) => f.write_str(s),
Self::AssignmentToConstant(s) if s.is_empty() => f.write_str("Cannot assign to a constant value"), Self::AssignmentToConstant(s) => match s.as_str() {
Self::AssignmentToConstant(s) => write!(f, "Cannot assign to constant '{}'", s), "" => f.write_str("Cannot assign to a constant value"),
s => write!(f, "Cannot assign to constant '{}'", s)
Self::AssignmentToInvalidLHS(s) if s.is_empty() => f.write_str("Expression cannot be assigned to"), },
Self::AssignmentToInvalidLHS(s) => f.write_str(s), Self::AssignmentToInvalidLHS(s) => match s.as_str() {
"" => f.write_str("Expression cannot be assigned to"),
s => f.write_str(s)
},
Self::LiteralTooLarge(typ, max) => write!(f, "{} exceeds the maximum limit ({})", typ, max), Self::LiteralTooLarge(typ, max) => write!(f, "{} exceeds the maximum limit ({})", typ, max),
Self::Reserved(s) => write!(f, "'{}' is a reserved keyword", s), Self::Reserved(s) => write!(f, "'{}' is a reserved keyword", s),