Improve error reporting for bad arguments to rust functions

This commit is contained in:
Filip Tibell 2023-06-08 14:52:17 +02:00
parent e2af4f51d2
commit 4d5bf6c2ae
No known key found for this signature in database
2 changed files with 26 additions and 28 deletions

View file

@ -7,7 +7,7 @@ use crate::{lua::table::TableBuilder, lua::task::TaskScheduler};
use super::task::TaskSchedulerAsyncExt;
const ASYNC_IMPL_LUA: &str = r#"
resumeAsync(thread(), ...)
resumeAsync(...)
return yield()
"#;
@ -43,21 +43,18 @@ impl LuaAsyncExt for &'static Lua {
let async_env_yield: LuaFunction = self.named_registry_value("co.yield")?;
let async_env = TableBuilder::new(self)?
.with_value("yield", async_env_yield)?
.with_function("thread", |lua, _: ()| Ok(lua.current_thread()))?
.with_function(
"resumeAsync",
move |lua: &Lua, (thread, args): (LuaThread, A)| {
let fut = func(lua, args);
let sched = lua
.app_data_ref::<&TaskScheduler>()
.expect("Missing task scheduler as a lua app data");
sched.queue_async_task(thread, None, async {
let rets = fut.await?;
let mult = rets.into_lua_multi(lua)?;
Ok(Some(mult))
})
},
)?
.with_function("resumeAsync", move |lua: &Lua, args: A| {
let thread = lua.current_thread();
let fut = func(lua, args);
let sched = lua
.app_data_ref::<&TaskScheduler>()
.expect("Missing task scheduler as a lua app data");
sched.queue_async_task(thread, None, async {
let rets = fut.await?;
let mult = rets.into_lua_multi(lua)?;
Ok(Some(mult))
})
})?
.build_readonly()?;
let async_func = self
.load(ASYNC_IMPL_LUA)

View file

@ -276,18 +276,16 @@ pub fn pretty_format_luau_error(e: &LuaError, colorized: bool) -> String {
)
}
}
LuaError::ToLuaConversionError { from, to, message } => {
let msg = message
.clone()
.map_or_else(String::new, |m| format!("\nDetails:\n\t{m}"));
format!("Failed to convert Rust type '{from}' into Luau type '{to}'!{msg}")
}
LuaError::FromLuaConversionError { from, to, message } => {
let msg = message
.clone()
.map_or_else(String::new, |m| format!("\nDetails:\n\t{m}"));
format!("Expected argument of type '{to}', got '{from}'!{msg}")
}
LuaError::BadArgument { pos, cause, .. } => match cause.as_ref() {
// TODO: Add more detail to this error message
LuaError::FromLuaConversionError { from, to, .. } => {
format!("Argument #{pos} must be of type '{to}', got '{from}'")
}
c => format!(
"Bad argument #{pos}\n{}",
pretty_format_luau_error(c, colorized)
),
},
e => format!("{e}"),
};
// Re-enable colors if they were previously enabled
@ -425,6 +423,7 @@ fn fix_error_nitpicks(full_message: String) -> String {
.replace("'require', Line 7", "'[C]' - function require")
.replace("'require', Line 8", "'[C]' - function require")
// Same thing here for our async script
.replace("'async', Line 2", "'[C]'")
.replace("'async', Line 3", "'[C]'")
// Fix error calls in custom script chunks coming through
.replace(
@ -436,6 +435,8 @@ fn fix_error_nitpicks(full_message: String) -> String {
"'[C]' - function require - function require",
"'[C]' - function require",
)
// Fix strange double C
.replace("'[C]'\n Script '[C]'", "'[C]'")
}
fn call_table_tostring_metamethod<'a>(tab: &'a LuaTable<'a>) -> Option<String> {