improve errors
This commit is contained in:
parent
783183975a
commit
20f2ad8fb1
4 changed files with 14 additions and 13 deletions
|
|
@ -294,8 +294,8 @@ const ENTITY_VAR_NAME: &str = "entity";
|
||||||
/// An error that can occur when internal [ScriptingPlugin] systems are being executed
|
/// An error that can occur when internal [ScriptingPlugin] systems are being executed
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum ScriptingError {
|
pub enum ScriptingError {
|
||||||
#[error("script runtime error: {0}\nscript backtrace:\n{1}")]
|
#[error("script runtime error: {0}")]
|
||||||
RuntimeError(String, String),
|
RuntimeError(String),
|
||||||
#[error("script compilation error: {0}")]
|
#[error("script compilation error: {0}")]
|
||||||
CompileError(Box<dyn std::error::Error + Send>),
|
CompileError(Box<dyn std::error::Error + Send>),
|
||||||
#[error("no runtime resource present")]
|
#[error("no runtime resource present")]
|
||||||
|
|
|
||||||
|
|
@ -187,7 +187,7 @@ impl Runtime for LuaRuntime {
|
||||||
.expect("Error clearing entity global variable");
|
.expect("Error clearing entity global variable");
|
||||||
result
|
result
|
||||||
})
|
})
|
||||||
.map_err(|e| ScriptingError::RuntimeError(e.to_string(), "".to_string()))?;
|
.map_err(|e| ScriptingError::RuntimeError(e.to_string()))?;
|
||||||
Ok(LuaScriptData)
|
Ok(LuaScriptData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -236,14 +236,14 @@ impl Runtime for LuaRuntime {
|
||||||
let func = engine
|
let func = engine
|
||||||
.globals()
|
.globals()
|
||||||
.get::<_, Function>(name)
|
.get::<_, Function>(name)
|
||||||
.map_err(|e| ScriptingError::RuntimeError(e.to_string(), "".to_string()))?;
|
.map_err(|e| ScriptingError::RuntimeError(e.to_string()))?;
|
||||||
let args = args
|
let args = args
|
||||||
.parse(engine)
|
.parse(engine)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|a| engine.registry_value::<mlua::Value>(&a.0).unwrap());
|
.map(|a| engine.registry_value::<mlua::Value>(&a.0).unwrap());
|
||||||
let result = func
|
let result = func
|
||||||
.call::<_, mlua::Value>(Variadic::from_iter(args))
|
.call::<_, mlua::Value>(Variadic::from_iter(args))
|
||||||
.map_err(|e| ScriptingError::RuntimeError(e.to_string(), "".to_string()))?;
|
.map_err(|e| ScriptingError::RuntimeError(e.to_string()))?;
|
||||||
engine
|
engine
|
||||||
.globals()
|
.globals()
|
||||||
.set(ENTITY_VAR_NAME, mlua::Value::Nil)
|
.set(ENTITY_VAR_NAME, mlua::Value::Nil)
|
||||||
|
|
@ -261,13 +261,13 @@ impl Runtime for LuaRuntime {
|
||||||
self.with_engine(|engine| {
|
self.with_engine(|engine| {
|
||||||
let val = engine
|
let val = engine
|
||||||
.registry_value::<Function>(&value.0)
|
.registry_value::<Function>(&value.0)
|
||||||
.map_err(|e| ScriptingError::RuntimeError(e.to_string(), "".to_string()))?;
|
.map_err(|e| ScriptingError::RuntimeError(e.to_string()))?;
|
||||||
let args = args
|
let args = args
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|a| engine.registry_value::<mlua::Value>(&a.0).unwrap());
|
.map(|a| engine.registry_value::<mlua::Value>(&a.0).unwrap());
|
||||||
let result = val
|
let result = val
|
||||||
.call::<_, mlua::Value>(Variadic::from_iter(args))
|
.call::<_, mlua::Value>(Variadic::from_iter(args))
|
||||||
.map_err(|e| ScriptingError::RuntimeError(e.to_string(), "".to_string()))?;
|
.map_err(|e| ScriptingError::RuntimeError(e.to_string()))?;
|
||||||
Ok(LuaValue::new(engine, result))
|
Ok(LuaValue::new(engine, result))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,7 @@ impl Runtime for RhaiRuntime {
|
||||||
|
|
||||||
engine
|
engine
|
||||||
.run_ast_with_scope(&mut scope, &ast)
|
.run_ast_with_scope(&mut scope, &ast)
|
||||||
.map_err(|e| ScriptingError::RuntimeError(e.to_string(), "".to_string()))?;
|
.map_err(|e| ScriptingError::RuntimeError(e.to_string()))?;
|
||||||
|
|
||||||
scope.remove::<BevyEntity>(ENTITY_VAR_NAME).unwrap();
|
scope.remove::<BevyEntity>(ENTITY_VAR_NAME).unwrap();
|
||||||
|
|
||||||
|
|
@ -156,7 +156,7 @@ impl Runtime for RhaiRuntime {
|
||||||
scope.remove::<BevyEntity>(ENTITY_VAR_NAME).unwrap();
|
scope.remove::<BevyEntity>(ENTITY_VAR_NAME).unwrap();
|
||||||
match result {
|
match result {
|
||||||
Ok(val) => Ok(RhaiValue(val)),
|
Ok(val) => Ok(RhaiValue(val)),
|
||||||
Err(e) => Err(ScriptingError::RuntimeError(e.to_string(), "".to_string())),
|
Err(e) => Err(ScriptingError::RuntimeError(e.to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -173,11 +173,11 @@ impl Runtime for RhaiRuntime {
|
||||||
|
|
||||||
let result = if args.len() == 1 && args.first().unwrap().0.is_unit() {
|
let result = if args.len() == 1 && args.first().unwrap().0.is_unit() {
|
||||||
f.call_raw(ctx, None, [])
|
f.call_raw(ctx, None, [])
|
||||||
.map_err(|e| ScriptingError::RuntimeError(e.to_string(), "".to_string()))?
|
.map_err(|e| ScriptingError::RuntimeError(e.to_string()))?
|
||||||
} else {
|
} else {
|
||||||
let args = args.into_iter().map(|a| a.0).collect::<Vec<Dynamic>>();
|
let args = args.into_iter().map(|a| a.0).collect::<Vec<Dynamic>>();
|
||||||
f.call_raw(ctx, None, args)
|
f.call_raw(ctx, None, args)
|
||||||
.map_err(|e| ScriptingError::RuntimeError(e.to_string(), "".to_string()))?
|
.map_err(|e| ScriptingError::RuntimeError(e.to_string()))?
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(RhaiValue(result))
|
Ok(RhaiValue(result))
|
||||||
|
|
|
||||||
|
|
@ -222,7 +222,8 @@ impl TryConvert for BevyVec3 {
|
||||||
|
|
||||||
impl From<magnus::Error> for ScriptingError {
|
impl From<magnus::Error> for ScriptingError {
|
||||||
fn from(value: magnus::Error) -> Self {
|
fn from(value: magnus::Error) -> Self {
|
||||||
ScriptingError::RuntimeError(
|
ScriptingError::RuntimeError(format!(
|
||||||
|
"error: {}\nbacktrace:\n{}\n",
|
||||||
value.inspect(),
|
value.inspect(),
|
||||||
value
|
value
|
||||||
.value()
|
.value()
|
||||||
|
|
@ -232,7 +233,7 @@ impl From<magnus::Error> for ScriptingError {
|
||||||
.to_vec::<String>()
|
.to_vec::<String>()
|
||||||
.expect("Failed to convert backtrace to vec of strings")
|
.expect("Failed to convert backtrace to vec of strings")
|
||||||
.join("\n"),
|
.join("\n"),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue