mlua
mlua copied to clipboard
Support rust backtraces on error
When writing a module it's pretty hard to debug where exactly the origin of the error is because there isn't a backtrace of the function. Are there any plans for such functionality?
Could you provide an example please?
// lib.rs
use mlua::prelude::*;
fn error_nested() -> LuaResult<()> {
Err(LuaError::external("Error from rust"))
}
fn error(_: &Lua, (): ()) -> LuaResult<()> {
Ok(error_nested()?)
}
#[mlua::lua_module]
fn libexample(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("error", lua.create_function(error)?)?;
Ok(exports)
}
-- init.lua
require("libexample").error()
# output
lua: callback error
stack traceback:
[C]: in function 'libexample.error'
init.lua:3: in main chunk
[C]: in ?
caused by: Error from rust
This is, in my opinion, simply not enough information to debug a complex project, so I'd like to know if there are plans to include rust backtraces alongside the lua ones (or at the very least line numbers of the line that originally threw the error in the "caused by:" line)
YES! PLEASE!
Even just the ability to add custom context to errors would already be a huge help.
Agreed, I've had some troubles with getting useful error messages from mlua
, especially for end-users who aren't writing the software. Right now I have a few very nasty hacks for my own error type including manually implementing Clone
for my own error enum and the following to try to wrangle errors out from Lua. I might be missing the obvious
impl From<Arc<mlua::Error>> for MyError {
fn from(err: Arc<mlua::Error>) -> Self {
match &*err {
// the only external errors are cloneable, but this really should not be this hacky
mlua::Error::ExternalError(ext) => match ext.downcast_ref::<MyError>() {
Some(e) => e.clone(),
None => MyError::LuaError(LuaError::ExternalError(err)),
},
_ => MyError::LuaError(LuaError::ExternalError(err)),
}
}
}
Even just the ability to add custom context to errors would already be a huge help.
@lenscas could you provide more details?
anyhow allows you to add more context to an error as you let the the error bubble up. If it would be possible to take this and similar contexts and put them inside a mlua::Error where lua will display it properly that could already help.
I added ErrorContext
trait in commit 8d8032173891bda1e1514e370cc4c60d9c157f30 that allow to attach additional context to errors.
I'm looking into adding backtraces (under a feature flag), the stable rust doesn't support getting backtraces, but it's possible using the backtrace crate.