Browser Panic Hook
A go-to panic handler for WebAssembly based Rust application is console_error_panic_hook, which does the job, but isn't really end user-friendly.
In the case of running the WebAssembly application as a frontend application, we do have the browser which can help interacting with the user, so why not leverage it.
That is what this crate does, present the panic to the user in a reasonable way.
Presentation
In order to keep things relatively simple, some basic HTML with some CSS classes is rendered. This can be used to control the styling of the representation.
In order to understand what can be styled, please take a look at the actual HTML.
NOTE: I most cases you will have a style sheet in place, as only basic HTML is rendered, it might actually be necessary to provide some styles, other overriding or adapting to your environment.
The panic is also always still logged to the console.
Usage
In a nutshell, you need to add the dependency and then set the handler once:
A more complete example can be found in the example folder.
Modes
Currently, there is only one mode of presenting/handling the panic, by replacing the full body content.
Additional modes in the future are possible, and should be configured using feature flags, as only one mode seems to make sense.
Future improvements
Additional improvements could be done, like:
- Call a diagnostic endpoint with the panic information
- Create a mode which overlays instead of replaces the HTML body
- Allow adding additional, application specific, HTML (like "click here to report the error")
- For sure some more …