diff --git a/src/error.rs b/src/error.rs index c8c5c63..a429a09 100644 --- a/src/error.rs +++ b/src/error.rs @@ -83,9 +83,12 @@ pub enum Error { /// [`UserData`]: trait.UserData.html UserDataBorrowMutError, /// A Rust callback returned `Err`, raising the contained `Error` as a Lua error. - /// - /// The first field is the Lua traceback, the second field holds the original error. - CallbackError(String, Arc), + CallbackError { + /// Lua call stack backtrace. + traceback: String, + /// Original error returned by the Rust code. + cause: Arc, + }, /// A custom error. /// /// This can be used for returning user-defined errors from callbacks. @@ -129,7 +132,7 @@ impl fmt::Display for Error { Error::UserDataTypeMismatch => write!(fmt, "Userdata not expected type"), Error::UserDataBorrowError => write!(fmt, "Userdata already mutably borrowed"), Error::UserDataBorrowMutError => write!(fmt, "Userdata already borrowed"), - Error::CallbackError(ref msg, _) => write!(fmt, "Error during lua callback: {}", msg), + Error::CallbackError { ref cause, .. } => write!(fmt, "{}", cause), Error::ExternalError(ref err) => err.fmt(fmt), } } @@ -148,14 +151,14 @@ impl StdError for Error { Error::UserDataTypeMismatch => "lua userdata type mismatch", Error::UserDataBorrowError => "lua userdata already mutably borrowed", Error::UserDataBorrowMutError => "lua userdata already borrowed", - Error::CallbackError(_, _) => "lua callback error", + Error::CallbackError { ref cause, .. } => cause.description(), Error::ExternalError(ref err) => err.description(), } } fn cause(&self) -> Option<&StdError> { match *self { - Error::CallbackError(_, ref cause) => Some(cause.as_ref()), + Error::CallbackError { ref cause, .. } => Some(cause.as_ref()), Error::ExternalError(ref err) => err.cause(), _ => None, } diff --git a/src/tests.rs b/src/tests.rs index 0706e2e..cb295ae 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -501,7 +501,7 @@ fn test_error() { _ => panic!("error not returned"), } match rust_error.call::<_, ()>(()) { - Err(Error::CallbackError(_, _)) => {} + Err(Error::CallbackError { .. }) => {} Err(_) => panic!("error is not CallbackError kind"), _ => panic!("error not returned"), } diff --git a/src/util.rs b/src/util.rs index d731df6..f8c2202 100644 --- a/src/util.rs +++ b/src/util.rs @@ -380,7 +380,10 @@ pub unsafe fn pcall_with_traceback( .to_str() .unwrap_or_else(|_| "") .to_owned(); - push_wrapped_error(state, Error::CallbackError(traceback, Arc::new(error))); + push_wrapped_error(state, Error::CallbackError { + traceback, + cause: Arc::new(error), + }); ffi::lua_remove(state, -2); } else if !is_wrapped_panic(state, 1) { let s = ffi::lua_tolstring(state, 1, ptr::null_mut()); @@ -415,7 +418,10 @@ pub unsafe fn resume_with_traceback( .to_str() .unwrap_or_else(|_| "") .to_owned(); - push_wrapped_error(state, Error::CallbackError(traceback, Arc::new(error))); + push_wrapped_error(state, Error::CallbackError { + traceback, + cause: Arc::new(error), + }); ffi::lua_remove(state, -2); } else if !is_wrapped_panic(state, 1) { let s = ffi::lua_tolstring(state, 1, ptr::null_mut());