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:
parent
c0ecc39fc7
commit
5c0f183a52
|
@ -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"
|
||||
|
|
11
src/error.rs
11
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 {
|
||||
|
|
|
@ -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"),
|
||||
}
|
||||
|
||||
|
|
16
src/util.rs
16
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
|
||||
|
|
Loading…
Reference in New Issue