Skip to content

Commit f0570d9

Browse files
authored
feat(core): improve run_mobile_plugin error handling (#6655)
1 parent be941b9 commit f0570d9

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Improve the `run_mobile_plugin` function error handling.

core/tauri/src/plugin/mobile.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ pub enum PluginInvokeError {
3838
Jni(#[from] jni::errors::Error),
3939
/// Error returned from direct mobile plugin invoke.
4040
#[error(transparent)]
41-
InvokeRejected(#[from] crate::plugin::mobile::ErrorResponse),
41+
InvokeRejected(#[from] ErrorResponse),
42+
/// Failed to deserialize response.
43+
#[error("failed to deserialize response: {0}")]
44+
CannotDeserializeResponse(serde_json::Error),
4245
}
4346

4447
/// Glue between Rust and the Kotlin code that sends the plugin response back.
@@ -289,10 +292,16 @@ impl<R: Runtime> PluginHandle<R> {
289292
crate::ios::PluginMessageCallback(plugin_method_response_handler),
290293
);
291294
}
292-
rx.recv()
293-
.unwrap()
294-
.map(|r| serde_json::from_value(r).unwrap())
295-
.map_err(|e| serde_json::from_value::<ErrorResponse>(e).unwrap().into())
295+
296+
let response = rx.recv().unwrap();
297+
match response {
298+
Ok(r) => serde_json::from_value(r).map_err(PluginInvokeError::CannotDeserializeResponse),
299+
Err(r) => Err(
300+
serde_json::from_value::<ErrorResponse>(r)
301+
.map(Into::into)
302+
.map_err(PluginInvokeError::CannotDeserializeResponse)?,
303+
),
304+
}
296305
}
297306

298307
// Executes the given Android method.
@@ -370,8 +379,13 @@ impl<R: Runtime> PluginHandle<R> {
370379
});
371380

372381
let response = rx.recv().unwrap()?;
373-
response
374-
.map(|r| serde_json::from_value(r).unwrap())
375-
.map_err(|e| serde_json::from_value::<ErrorResponse>(e).unwrap().into())
382+
match response {
383+
Ok(r) => serde_json::from_value(r).map_err(PluginInvokeError::CannotDeserializeResponse),
384+
Err(r) => Err(
385+
serde_json::from_value::<ErrorResponse>(r)
386+
.map(Into::into)
387+
.map_err(PluginInvokeError::CannotDeserializeResponse)?,
388+
),
389+
}
376390
}
377391
}

0 commit comments

Comments
 (0)