feat(core): improve run_mobile_plugin error handling (#6655)

This commit is contained in:
Lucas Fernandes Nogueira
2023-04-07 13:08:25 -07:00
committed by GitHub
parent be941b9719
commit f0570d9fee
2 changed files with 27 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
Improve the `run_mobile_plugin` function error handling.

View File

@@ -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<R: Runtime> PluginHandle<R> {
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::<ErrorResponse>(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::<ErrorResponse>(r)
.map(Into::into)
.map_err(PluginInvokeError::CannotDeserializeResponse)?,
),
}
}
// Executes the given Android method.
@@ -370,8 +379,13 @@ impl<R: Runtime> PluginHandle<R> {
});
let response = rx.recv().unwrap()?;
response
.map(|r| serde_json::from_value(r).unwrap())
.map_err(|e| serde_json::from_value::<ErrorResponse>(e).unwrap().into())
match response {
Ok(r) => serde_json::from_value(r).map_err(PluginInvokeError::CannotDeserializeResponse),
Err(r) => Err(
serde_json::from_value::<ErrorResponse>(r)
.map(Into::into)
.map_err(PluginInvokeError::CannotDeserializeResponse)?,
),
}
}
}