Add position to script error.
This commit is contained in:
18
src/api.rs
18
src/api.rs
@@ -437,13 +437,23 @@ impl Engine {
|
||||
/// Read the contents of a file into a string.
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
fn read_file(path: PathBuf) -> Result<String, Box<EvalAltResult>> {
|
||||
let mut f = File::open(path.clone())
|
||||
.map_err(|err| Box::new(EvalAltResult::ErrorReadingScriptFile(path.clone(), err)))?;
|
||||
let mut f = File::open(path.clone()).map_err(|err| {
|
||||
Box::new(EvalAltResult::ErrorReadingScriptFile(
|
||||
path.clone(),
|
||||
Position::none(),
|
||||
err,
|
||||
))
|
||||
})?;
|
||||
|
||||
let mut contents = String::new();
|
||||
|
||||
f.read_to_string(&mut contents)
|
||||
.map_err(|err| Box::new(EvalAltResult::ErrorReadingScriptFile(path.clone(), err)))?;
|
||||
f.read_to_string(&mut contents).map_err(|err| {
|
||||
Box::new(EvalAltResult::ErrorReadingScriptFile(
|
||||
path.clone(),
|
||||
Position::none(),
|
||||
err,
|
||||
))
|
||||
})?;
|
||||
|
||||
Ok(contents)
|
||||
}
|
||||
|
@@ -1530,7 +1530,10 @@ impl Engine {
|
||||
.eval_expr(scope, state, fn_lib, expr, level)?
|
||||
.try_cast::<String>()
|
||||
{
|
||||
let module = self.module_resolver.resolve(self, &path)?;
|
||||
let module = self
|
||||
.module_resolver
|
||||
.resolve(self, &path)
|
||||
.map_err(|err| EvalAltResult::set_position(err, expr.position()))?;
|
||||
|
||||
// TODO - avoid copying module name in inner block?
|
||||
let mod_name = name.as_ref().clone();
|
||||
|
@@ -29,7 +29,7 @@ pub enum EvalAltResult {
|
||||
///
|
||||
/// Never appears under the `no_std` feature.
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
ErrorReadingScriptFile(PathBuf, std::io::Error),
|
||||
ErrorReadingScriptFile(PathBuf, Position, std::io::Error),
|
||||
|
||||
/// Call to an unknown function. Wrapped value is the name of the function.
|
||||
ErrorFunctionNotFound(String, Position),
|
||||
@@ -94,7 +94,7 @@ impl EvalAltResult {
|
||||
pub(crate) fn desc(&self) -> &str {
|
||||
match self {
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
Self::ErrorReadingScriptFile(_, _) => "Cannot read from script file",
|
||||
Self::ErrorReadingScriptFile(_, _, _) => "Cannot read from script file",
|
||||
|
||||
Self::ErrorParsing(p) => p.desc(),
|
||||
Self::ErrorFunctionNotFound(_, _) => "Function not found",
|
||||
@@ -150,9 +150,13 @@ impl fmt::Display for EvalAltResult {
|
||||
|
||||
match self {
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
Self::ErrorReadingScriptFile(path, err) => {
|
||||
Self::ErrorReadingScriptFile(path, pos, err) if pos.is_none() => {
|
||||
write!(f, "{} '{}': {}", desc, path.display(), err)
|
||||
}
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
Self::ErrorReadingScriptFile(path, pos, err) => {
|
||||
write!(f, "{} '{}': {} ({})", desc, path.display(), err, pos)
|
||||
}
|
||||
|
||||
Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p),
|
||||
|
||||
@@ -261,7 +265,7 @@ impl EvalAltResult {
|
||||
pub fn position(&self) -> Position {
|
||||
match self {
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
Self::ErrorReadingScriptFile(_, _) => Position::none(),
|
||||
Self::ErrorReadingScriptFile(_, pos, _) => *pos,
|
||||
|
||||
Self::ErrorParsing(err) => err.position(),
|
||||
|
||||
@@ -297,7 +301,7 @@ impl EvalAltResult {
|
||||
pub(crate) fn set_position(mut err: Box<Self>, new_position: Position) -> Box<Self> {
|
||||
match err.as_mut() {
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
Self::ErrorReadingScriptFile(_, _) => (),
|
||||
Self::ErrorReadingScriptFile(_, pos, _) => *pos = new_position,
|
||||
|
||||
Self::ErrorParsing(err) => err.1 = new_position,
|
||||
|
||||
|
Reference in New Issue
Block a user