Repository: tauri-apps/wry (language: rust)

Issue:
Better error message for invalid custom scheme name on webkit
**Is your feature request related to a problem? Please describe.**

When registering an invalid scheme like so `.register_asynchronous_uri_scheme_protocol("in_valid", ...)`, the error (on macos) is:
```
thread 'main' panicked at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wry-0.40.1/src/wkwebview/mod.rs:403:18:
Uncaught exception <NSException: 0x149f0c280>
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
libc++abi: terminating due to uncaught foreign exception
```

**Describe the solution you'd like**
Either the scheme name should be validated on the Rust end beforehand or the error from webkit should be handled better.


**Additional context**
Tested on tauri 2.0.0-beta
Better error message for invalid custom scheme name on webkit
**Is your feature request related to a problem? Please describe.**

When registering an invalid scheme like so `.register_asynchronous_uri_scheme_protocol("in_valid", ...)`, the error (on macos) is:
```
thread 'main' panicked at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wry-0.40.1/src/wkwebview/mod.rs:403:18:
Uncaught exception <NSException: 0x149f0c280>
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
libc++abi: terminating due to uncaught foreign exception
```

**Describe the solution you'd like**
Either the scheme name should be validated on the Rust end beforehand or the error from webkit should be handled better.


**Additional context**
Tested on tauri 2.0.0-beta


Discussion / hints:
Thanks for the report!
Currently, the `objc` crate we are using can't tell which `NSException` it is, so we don't know if the thrown exception is either the scheme name is invalid, or it's already been registered. Checking the name before using it looks more promising for now.
Honestly, even `The schema "in_valid" is invalid or already registered` would be enough in my opinion. The issue right now is that it doesn't point to scheme registration at all
Thanks for the report!
Currently, the `objc` crate we are using can't tell which `NSException` it is, so we don't know if the thrown exception is either the scheme name is invalid, or it's already been registered. Checking the name before using it looks more promising for now.
Honestly, even `The schema "in_valid" is invalid or already registered` would be enough in my opinion. The issue right now is that it doesn't point to scheme registration at all

Fix patch:
diff --git a/.changes/chore-mac-add-url-scheme-register-error.md b/.changes/chore-mac-add-url-scheme-register-error.md
new file mode 100644
index 000000000..b9d6313b8
--- /dev/null
+++ b/.changes/chore-mac-add-url-scheme-register-error.md
@@ -0,0 +1,5 @@
+---
+"wry": patch
+---
+
+On macOS, emit an error when the URL scheme registration fails.
diff --git a/src/error.rs b/src/error.rs
index a0b2518ed..075a893a4 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -59,4 +59,6 @@ pub enum Error {
   CrossBeamRecvError(#[from] crossbeam_channel::RecvError),
   #[error("Custom protocol task is invalid.")]
   CustomProtocolTaskInvalid,
+  #[error("Failed to register URL scheme: {0}, could be due to invalid URL scheme or the scheme is already registered.")]
+  UrlSchemeRegisterError(String),
 }
diff --git a/src/wkwebview/mod.rs b/src/wkwebview/mod.rs
index 0da98f4c5..01a0d3477 100644
--- a/src/wkwebview/mod.rs
+++ b/src/wkwebview/mod.rs
@@ -423,7 +423,13 @@ impl InnerWebView {
 
         (*handler).set_ivar("function", function as *mut _ as *mut c_void);
         (*handler).set_ivar("webview_id", webview_id);
-        let () = msg_send![config, setURLSchemeHandler:handler forURLScheme:NSString::new(&name)];
+
+        (*config)
+          .send_message::<(id, NSString), ()>(
+            sel!(setURLSchemeHandler:forURLScheme:),
+            (handler, NSString::new(&name)),
+          )
+          .map_err(|_| crate::Error::UrlSchemeRegisterError(name))?;
       }
 
       // WebView and manager


Test patch: