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.
This commit is contained in:
kyren 2017-05-22 14:24:19 -04:00
parent c0ecc39fc7
commit 5c0f183a52
4 changed files with 25 additions and 10 deletions

View File

@ -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"

View File

@ -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 {

View File

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

View File

@ -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