diff --git a/tcx/src/error_handling.rs b/tcx/src/error_handling.rs index 26755d97..4a768afb 100644 --- a/tcx/src/error_handling.rs +++ b/tcx/src/error_handling.rs @@ -11,15 +11,17 @@ thread_local! { #[cfg_attr(tarpaulin, skip)] #[allow(irrefutable_let_patterns)] -fn notify_err(err: Error) { +fn notify_err(err: Error) -> Error { if let _backtrace = err.backtrace() { LAST_BACKTRACE.with(|e| { *e.borrow_mut() = Some((None, Backtrace::new())); }); } + let display_err = format_err!("{}", &err); LAST_ERROR.with(|e| { *e.borrow_mut() = Some(err); }); + display_err } fn lock_all_keystore() { @@ -32,11 +34,11 @@ fn lock_all_keystore() { /// catch any error and format to string /// ref: #[cfg_attr(tarpaulin, skip)] -pub unsafe fn landingpad Result + panic::UnwindSafe, T>(f: F) -> T { +pub unsafe fn landingpad Result + panic::UnwindSafe, T>(f: F) -> Result { match panic::catch_unwind(f) { Ok(rv) => { lock_all_keystore(); - rv.map_err(notify_err).unwrap_or_else(|_| mem::zeroed()) + rv.map_err(notify_err) } Err(err) => { lock_all_keystore(); @@ -49,8 +51,7 @@ pub unsafe fn landingpad Result + panic::UnwindSafe, T>(f: F) None => "Box", }, }; - notify_err(format_err!("{}", msg)); - mem::zeroed() + Err(notify_err(format_err!("{}", msg))) } } } diff --git a/tcx/src/lib.rs b/tcx/src/lib.rs index ad809ffe..50178e9e 100644 --- a/tcx/src/lib.rs +++ b/tcx/src/lib.rs @@ -10,6 +10,8 @@ use crate::api::{Response, TcxAction}; pub mod error_handling; pub mod handler; +use failure::Error; +use std::result; use crate::error_handling::{landingpad, LAST_BACKTRACE, LAST_ERROR}; #[allow(deprecated)] @@ -39,6 +41,8 @@ lazy_static! { pub static ref IS_DEBUG: RwLock = RwLock::new(false); } +pub type Result = result::Result; + #[no_mangle] pub unsafe extern "C" fn free_const_string(s: *const c_char) { if s.is_null() { @@ -57,7 +61,7 @@ pub unsafe extern "C" fn call_tcx_api(hex_str: *const c_char) -> *const c_char { let data = hex::decode(hex_str).expect("parse_arguments hex decode"); let action: TcxAction = TcxAction::decode(data.as_slice()).expect("decode tcx api"); - let reply: Vec = match action.method.to_lowercase().as_str() { + let reply: Result> = match action.method.to_lowercase().as_str() { "init_token_core_x" => landingpad(|| { handler::init_token_core_x(&action.param.unwrap().value).unwrap(); Ok(vec![]) @@ -117,9 +121,13 @@ pub unsafe extern "C" fn call_tcx_api(hex_str: *const c_char) -> *const c_char { "unlock_then_crash" => landingpad(|| unlock_then_crash(&action.param.unwrap().value)), _ => landingpad(|| Err(format_err!("unsupported_method"))), }; - - let ret_str = hex::encode(reply); - CString::new(ret_str).unwrap().into_raw() + match reply { + Ok(reply) => { + let ret_str = hex::encode(reply); + CString::new(ret_str).unwrap().into_raw() + } + _ => CString::new("").unwrap().into_raw(), + } } #[no_mangle] @@ -136,13 +144,7 @@ pub unsafe extern "C" fn clear_err() { pub unsafe extern "C" fn get_last_err_message() -> *const c_char { LAST_ERROR.with(|e| { if let Some(ref err) = *e.borrow() { - let rsp = Response { - is_success: false, - error: err.to_string(), - }; - let rsp_bytes = encode_message(rsp).expect("encode error"); - let ret_str = hex::encode(rsp_bytes); - CString::new(ret_str).unwrap().into_raw() + CString::new(err.to_string()).unwrap().into_raw() } else { CString::new("").unwrap().into_raw() }