Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A panic hook for frontend applications

License

NotificationsYou must be signed in to change notification settings

ctron/browser-panic-hook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CIGitHub release (latest SemVer)crates.iodocs.rs

A go-to panic handler for WebAssembly based Rust application isconsole_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 helpinteracting with the user, so why not leverage it.

That is what this crate does, failing in style:

Screenshot of an example

Presentation

The panic is also always still logged to the console as a first step. As it might happen that running the panichandler might run into trouble as well.

Next, it depends on what presentation mode you register. There is aBasic one, which takes over the body of thedocument and renders some simple HTML structure, including some CSS class names. In combination with an existingstylesheet, this may already be enough.

TheCustomBody mode allow to provide a function, which renders the full body. This can be used to create morecustomized output for frameworks like PatternFly or Bootstrap.

Everything boils down to a trait (PresentationMode), for which you can also provide a custom implementation.

Just note, your application is already panicking, so you should try to keep it simple, or rely on some basicbrowser functionality.

Usage

In any case, you need to add this crate a dependency to your project, and register the panic handler. Registeringcan be as easy as:

pubfnmain() ->Result<(),JsValue>{    browser_panic_hook::set_once_default();// run your application ...Ok(())}

You can also use a more customized variant:

pubfnmain() ->Result<(),JsValue>{     browser_panic_hook::set_once(||{        browser_panic_hook::CustomBody(Box::new(|details|{// render new bodyformat!("…")}))});// run your application ...Ok(())}

More complete examples can be found in theexamples folder.

Yew

Yew already sets a default panic hook. This can be overridden using:

pubfnmain() ->Result<(),JsValue>{// provide a custom panic hook    yew::set_custom_panic_hook(Basic.into_panic_hook());// run the application    yew::Renderer::<app::Application>::new().render();Ok(())}

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 …

About

A panic hook for frontend applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp