mlua icon indicating copy to clipboard operation
mlua copied to clipboard

Support rust backtraces on error

Open Delta-official opened this issue 2 years ago • 7 comments

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?

Delta-official avatar Dec 01 '22 12:12 Delta-official

Could you provide an example please?

khvzak avatar Dec 02 '22 00:12 khvzak

// 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)

Delta-official avatar Dec 02 '22 01:12 Delta-official

YES! PLEASE!

Even just the ability to add custom context to errors would already be a huge help.

lenscas avatar Dec 21 '22 11:12 lenscas

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)),
        }
    }
}

sondr3 avatar Dec 21 '22 12:12 sondr3

Even just the ability to add custom context to errors would already be a huge help.

@lenscas could you provide more details?

khvzak avatar Mar 16 '23 00:03 khvzak

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.

lenscas avatar Mar 16 '23 14:03 lenscas

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.

khvzak avatar Mar 17 '23 01:03 khvzak