Deprecate From<EvalAltResult> for Result<T, Box<EvalAltResult>> because it is clearer for code to explicitly wrap errors in Err.

This commit is contained in:
Stephen Chung
2021-10-19 23:52:58 +08:00
parent 6d31bb0d19
commit 3001e90775
23 changed files with 226 additions and 193 deletions

View File

@@ -115,11 +115,11 @@ pub fn ensure_no_data_race(
.skip(if is_method_call { 1 } else { 0 })
.find(|(_, a)| a.is_locked())
{
return EvalAltResult::ErrorDataRace(
return Err(EvalAltResult::ErrorDataRace(
format!("argument #{} of function '{}'", n + 1, fn_name),
Position::NONE,
)
.into();
.into());
}
Ok(())
@@ -392,7 +392,7 @@ impl Engine {
crate::engine::FN_IDX_GET => {
assert!(args.len() == 2);
EvalAltResult::ErrorIndexingType(
Err(EvalAltResult::ErrorIndexingType(
format!(
"{} [{}]",
self.map_type_name(args[0].type_name()),
@@ -400,7 +400,7 @@ impl Engine {
),
pos,
)
.into()
.into())
}
// index setter function not found?
@@ -408,7 +408,7 @@ impl Engine {
crate::engine::FN_IDX_SET => {
assert!(args.len() == 3);
EvalAltResult::ErrorIndexingType(
Err(EvalAltResult::ErrorIndexingType(
format!(
"{} [{}] = {}",
self.map_type_name(args[0].type_name()),
@@ -417,7 +417,7 @@ impl Engine {
),
pos,
)
.into()
.into())
}
// Getter function not found?
@@ -425,7 +425,7 @@ impl Engine {
_ if name.starts_with(crate::engine::FN_GET) => {
assert!(args.len() == 1);
EvalAltResult::ErrorDotExpr(
Err(EvalAltResult::ErrorDotExpr(
format!(
"Unknown property '{}' - a getter is not registered for type '{}'",
&name[crate::engine::FN_GET.len()..],
@@ -433,7 +433,7 @@ impl Engine {
),
pos,
)
.into()
.into())
}
// Setter function not found?
@@ -441,7 +441,7 @@ impl Engine {
_ if name.starts_with(crate::engine::FN_SET) => {
assert!(args.len() == 2);
EvalAltResult::ErrorDotExpr(
Err(EvalAltResult::ErrorDotExpr(
format!(
"No writable property '{}' - a setter is not registered for type '{}' to handle '{}'",
&name[crate::engine::FN_SET.len()..],
@@ -450,14 +450,15 @@ impl Engine {
),
pos,
)
.into()
.into())
}
// Raise error
_ => {
EvalAltResult::ErrorFunctionNotFound(self.gen_call_signature(None, name, args), pos)
.into()
}
_ => Err(EvalAltResult::ErrorFunctionNotFound(
self.gen_call_signature(None, name, args),
pos,
)
.into()),
}
}
@@ -489,7 +490,7 @@ impl Engine {
err: Box<EvalAltResult>,
pos: Position,
) -> RhaiResult {
EvalAltResult::ErrorInFunctionCall(
Err(EvalAltResult::ErrorInFunctionCall(
name,
fn_def
.lib
@@ -500,7 +501,7 @@ impl Engine {
err,
pos,
)
.into()
.into())
}
#[cfg(not(feature = "unchecked"))]
@@ -514,7 +515,7 @@ impl Engine {
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "unchecked"))]
if level > self.max_call_levels() {
return EvalAltResult::ErrorStackOverflow(pos).into();
return Err(EvalAltResult::ErrorStackOverflow(pos).into());
}
let orig_scope_level = state.scope_level;
@@ -578,7 +579,7 @@ impl Engine {
// System errors are passed straight-through
mut err if err.is_system_exception() => {
err.set_position(pos);
err.into()
Err(err.into())
}
// Other errors are wrapped in `ErrorInFunctionCall`
_ => make_error(fn_def.name.to_string(), fn_def, state, err, pos),
@@ -651,7 +652,7 @@ impl Engine {
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
fn no_method_err(name: &str, pos: Position) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
let msg = format!("'{0}' should not be called this way. Try {0}(...);", name);
EvalAltResult::ErrorRuntime(msg.into(), pos).into()
Err(EvalAltResult::ErrorRuntime(msg.into(), pos).into())
}
// Check for data race.
@@ -1458,11 +1459,11 @@ impl Engine {
Some(f) => unreachable!("unknown function type: {:?}", f),
None => EvalAltResult::ErrorFunctionNotFound(
None => Err(EvalAltResult::ErrorFunctionNotFound(
self.gen_call_signature(Some(namespace), fn_name, &args),
pos,
)
.into(),
.into()),
}
}
}