From f0570d9feee05792cc720d26ef32da5eaed7f797 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Fri, 7 Apr 2023 13:08:25 -0700 Subject: [PATCH] feat(core): improve `run_mobile_plugin` error handling (#6655) --- .../improve-mobile-plugin-error-handling.md | 5 ++++ core/tauri/src/plugin/mobile.rs | 30 ++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 .changes/improve-mobile-plugin-error-handling.md diff --git a/.changes/improve-mobile-plugin-error-handling.md b/.changes/improve-mobile-plugin-error-handling.md new file mode 100644 index 000000000..71c57af54 --- /dev/null +++ b/.changes/improve-mobile-plugin-error-handling.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Improve the `run_mobile_plugin` function error handling. diff --git a/core/tauri/src/plugin/mobile.rs b/core/tauri/src/plugin/mobile.rs index e1501cf7a..fefa4dc1b 100644 --- a/core/tauri/src/plugin/mobile.rs +++ b/core/tauri/src/plugin/mobile.rs @@ -38,7 +38,10 @@ pub enum PluginInvokeError { Jni(#[from] jni::errors::Error), /// Error returned from direct mobile plugin invoke. #[error(transparent)] - InvokeRejected(#[from] crate::plugin::mobile::ErrorResponse), + InvokeRejected(#[from] ErrorResponse), + /// Failed to deserialize response. + #[error("failed to deserialize response: {0}")] + CannotDeserializeResponse(serde_json::Error), } /// Glue between Rust and the Kotlin code that sends the plugin response back. @@ -289,10 +292,16 @@ impl PluginHandle { crate::ios::PluginMessageCallback(plugin_method_response_handler), ); } - rx.recv() - .unwrap() - .map(|r| serde_json::from_value(r).unwrap()) - .map_err(|e| serde_json::from_value::(e).unwrap().into()) + + let response = rx.recv().unwrap(); + match response { + Ok(r) => serde_json::from_value(r).map_err(PluginInvokeError::CannotDeserializeResponse), + Err(r) => Err( + serde_json::from_value::(r) + .map(Into::into) + .map_err(PluginInvokeError::CannotDeserializeResponse)?, + ), + } } // Executes the given Android method. @@ -370,8 +379,13 @@ impl PluginHandle { }); let response = rx.recv().unwrap()?; - response - .map(|r| serde_json::from_value(r).unwrap()) - .map_err(|e| serde_json::from_value::(e).unwrap().into()) + match response { + Ok(r) => serde_json::from_value(r).map_err(PluginInvokeError::CannotDeserializeResponse), + Err(r) => Err( + serde_json::from_value::(r) + .map(Into::into) + .map_err(PluginInvokeError::CannotDeserializeResponse)?, + ), + } } }