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; use super::task::TaskSchedulerAsyncExt;
const ASYNC_IMPL_LUA: &str = r#" const ASYNC_IMPL_LUA: &str = r#"
resumeAsync(thread(), ...) resumeAsync(...)
return yield() 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_yield: LuaFunction = self.named_registry_value("co.yield")?;
let async_env = TableBuilder::new(self)? let async_env = TableBuilder::new(self)?
.with_value("yield", async_env_yield)? .with_value("yield", async_env_yield)?
.with_function("thread", |lua, _: ()| Ok(lua.current_thread()))? .with_function("resumeAsync", move |lua: &Lua, args: A| {
.with_function( let thread = lua.current_thread();
"resumeAsync", let fut = func(lua, args);
move |lua: &Lua, (thread, args): (LuaThread, A)| { let sched = lua
let fut = func(lua, args); .app_data_ref::<&TaskScheduler>()
let sched = lua .expect("Missing task scheduler as a lua app data");
.app_data_ref::<&TaskScheduler>() sched.queue_async_task(thread, None, async {
.expect("Missing task scheduler as a lua app data"); let rets = fut.await?;
sched.queue_async_task(thread, None, async { let mult = rets.into_lua_multi(lua)?;
let rets = fut.await?; Ok(Some(mult))
let mult = rets.into_lua_multi(lua)?; })
Ok(Some(mult)) })?
})
},
)?
.build_readonly()?; .build_readonly()?;
let async_func = self let async_func = self
.load(ASYNC_IMPL_LUA) .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 } => { LuaError::BadArgument { pos, cause, .. } => match cause.as_ref() {
let msg = message // TODO: Add more detail to this error message
.clone() LuaError::FromLuaConversionError { from, to, .. } => {
.map_or_else(String::new, |m| format!("\nDetails:\n\t{m}")); format!("Argument #{pos} must be of type '{to}', got '{from}'")
format!("Failed to convert Rust type '{from}' into Luau type '{to}'!{msg}") }
} c => format!(
LuaError::FromLuaConversionError { from, to, message } => { "Bad argument #{pos}\n{}",
let msg = message pretty_format_luau_error(c, colorized)
.clone() ),
.map_or_else(String::new, |m| format!("\nDetails:\n\t{m}")); },
format!("Expected argument of type '{to}', got '{from}'!{msg}")
}
e => format!("{e}"), e => format!("{e}"),
}; };
// Re-enable colors if they were previously enabled // 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 7", "'[C]' - function require")
.replace("'require', Line 8", "'[C]' - function require") .replace("'require', Line 8", "'[C]' - function require")
// Same thing here for our async script // Same thing here for our async script
.replace("'async', Line 2", "'[C]'")
.replace("'async', Line 3", "'[C]'") .replace("'async', Line 3", "'[C]'")
// Fix error calls in custom script chunks coming through // Fix error calls in custom script chunks coming through
.replace( .replace(
@ -436,6 +435,8 @@ fn fix_error_nitpicks(full_message: String) -> String {
"'[C]' - function require - function require", "'[C]' - function require - function require",
"'[C]' - 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> { fn call_table_tostring_metamethod<'a>(tab: &'a LuaTable<'a>) -> Option<String> {