diff options
author | kyren <kerriganw@gmail.com> | 2017-05-22 14:24:19 -0400 |
---|---|---|
committer | kyren <kerriganw@gmail.com> | 2017-05-22 14:25:36 -0400 |
commit | 5c0f183a52ec65bf0c5422cc849138a457e0a5eb (patch) | |
tree | 2e103f819c2ba1e2a83d3fe2e8b09bee77510547 | |
parent | c0ecc39fc7e065d0219d2efa321729b29b9016ca (diff) | |
download | mlua-5c0f183a52ec65bf0c5422cc849138a457e0a5eb.zip |
Improved error handling and formatting
Now prints lua backtraces on callback errors as well. This could be
controlled with LUA_BACKTRACE or just RUST_BACKTRACE or similar.
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/error.rs | 11 | ||||
-rw-r--r-- | src/tests.rs | 6 | ||||
-rw-r--r-- | src/util.rs | 16 |
4 files changed, 25 insertions, 10 deletions
@@ -1,6 +1,6 @@ [package] name = "rlua" -version = "0.2.0" +version = "0.3.0" authors = ["kyren <catherine@chucklefish.org>"] repository = "https://github.com/chucklefish/rlua" description = "High level bindings to Lua 5.3" diff --git a/src/error.rs b/src/error.rs index a5c2ed8..490e1c6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,8 +30,15 @@ error_chain! { } errors { - ScriptError(err: String) - IncompleteStatement(err: String) + ScriptError(err: String) { + display("Error executing lua script {}", err) + } + CallbackError(err: String) { + display("Error during lua callback {}", err) + } + IncompleteStatement(err: String) { + display("Incomplete lua statement {}", err) + } } foreign_links { diff --git a/src/tests.rs b/src/tests.rs index 3fd3d82..2ec8a90 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -388,10 +388,8 @@ fn test_error() { _ => panic!("error not thrown"), } match rust_error.call::<_, ()>(()) { - Err(LuaError(LuaErrorKind::ExternalError(e), _)) => { - assert_eq!(e.description(), "test error") - } - Err(_) => panic!("error is not ExternalError kind"), + Err(LuaError(LuaErrorKind::CallbackError(_), _)) => {} + Err(_) => panic!("error is not CallbackError kind"), _ => panic!("error not thrown"), } diff --git a/src/util.rs b/src/util.rs index 99b6733..ad8bf60 100644 --- a/src/util.rs +++ b/src/util.rs @@ -140,12 +140,22 @@ pub unsafe fn pcall_with_traceback(state: *mut ffi::lua_State, nresults: c_int) -> c_int { unsafe extern "C" fn message_handler(state: *mut ffi::lua_State) -> c_int { - if !is_wrapped_error(state, 1) { - let s = ffi::lua_tolstring(state, -1, ptr::null_mut()); + if is_wrapped_error(state, 1) { + if !is_panic_error(state, 1) { + let error = pop_error(state); + ffi::luaL_traceback(state, state, ptr::null(), 0); + let traceback = CStr::from_ptr(ffi::lua_tolstring(state, 1, ptr::null_mut())) + .to_str() + .unwrap() + .to_owned(); + push_error(state, WrappedError::Error(LuaError::with_chain(error, LuaErrorKind::CallbackError(traceback)))); + } + } else { + let s = ffi::lua_tolstring(state, 1, ptr::null_mut()); if !s.is_null() { ffi::luaL_traceback(state, state, s, 0); } else { - ffi::luaL_traceback(state, state, cstr!("<unprintable error>"), 0); + ffi::luaL_traceback(state, state, cstr!("<unprintable lua error>"), 0); } } 1 |