{"id":13807542,"url":"https://github.com/ctron/browser-panic-hook","last_synced_at":"2025-04-21T03:30:59.925Z","repository":{"id":171887687,"uuid":"648567558","full_name":"ctron/browser-panic-hook","owner":"ctron","description":"A panic hook for frontend applications","archived":false,"fork":false,"pushed_at":"2023-06-02T13:57:18.000Z","size":111,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-01T08:11:51.058Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ctron.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-06-02T09:15:41.000Z","updated_at":"2024-09-01T12:02:30.000Z","dependencies_parsed_at":"2024-01-22T07:08:27.881Z","dependency_job_id":"6698de9d-c0b6-451f-b9a7-9225e515d52d","html_url":"https://github.com/ctron/browser-panic-hook","commit_stats":null,"previous_names":["ctron/browser-panic-hook"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctron%2Fbrowser-panic-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctron%2Fbrowser-panic-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctron%2Fbrowser-panic-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctron%2Fbrowser-panic-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ctron","download_url":"https://codeload.github.com/ctron/browser-panic-hook/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248854416,"owners_count":21172358,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T01:01:26.608Z","updated_at":"2025-04-21T03:30:59.658Z","avatar_url":"https://github.com/ctron.png","language":"Rust","funding_links":[],"categories":["Crates"],"sub_categories":["Utils"],"readme":"# Browser Panic Hook\n\n[![CI](https://github.com/ctron/browser-panic-hook/workflows/CI/badge.svg)](https://github.com/ctron/browser-panic-hook/actions?query=workflow%3A%22CI%22)\n[![GitHub release (latest SemVer)](https://img.shields.io/github/v/tag/ctron/browser-panic-hook?sort=semver)](https://github.com/ctron/browser-panic-hook/releases)\n[![crates.io](https://img.shields.io/crates/v/browser-panic-hook.svg)](https://crates.io/crates/browser-panic-hook)\n[![docs.rs](https://docs.rs/browser-panic-hook/badge.svg)](https://docs.rs/browser-panic-hook)\n\nA go-to panic handler for WebAssembly based Rust application is [console_error_panic_hook](https://github.com/rustwasm/console_error_panic_hook), which does the job, but isn't really end user-friendly.\n\nIn the case of running the WebAssembly application as a frontend application, we do have the browser which can help\ninteracting with the user, so why not leverage it.\n\nThat is what this crate does, failing in style:\n\n![Screenshot of an example](docs/example1.png)\n\n## Presentation\n\nThe panic is also always still logged to the console as a first step. As it might happen that running the panic\nhandler might run into trouble as well.\n\nNext, it depends on what presentation mode you register. There is a `Basic` one, which takes over the body of the\ndocument and renders some simple HTML structure, including some CSS class names. In combination with an existing\nstylesheet, this may already be enough.\n\nThe `CustomBody` mode allow to provide a function, which renders the full body. This can be used to create more\ncustomized output for frameworks like PatternFly or Bootstrap.\n\nEverything boils down to a trait (`PresentationMode`), for which you can also provide a custom implementation.\n\nJust note, your application is already panicking, so you should try to keep it simple, or rely on some basic\nbrowser functionality.\n\n## Usage\n\nIn any case, you need to add this crate a dependency to your project, and register the panic handler. Registering\ncan be as easy as:\n\n```rust\npub fn main() -\u003e Result\u003c(), JsValue\u003e {\n    browser_panic_hook::set_once_default();\n\n    // run your application ...\n    Ok(())\n}\n```\n\nYou can also use a more customized variant:\n\n```rust\npub fn main() -\u003e Result\u003c(), JsValue\u003e { \n    browser_panic_hook::set_once(|| {\n        browser_panic_hook::CustomBody(Box::new(|details| {\n            // render new body\n            format!(\"…\")\n        }))\n    });\n \n    // run your application ...\n    Ok(())\n}\n```\n\nMore complete examples can be found in the [examples](examples) folder.\n\n## Yew\n\nYew already sets a default panic hook. This can be overridden using:\n\n```rust\npub fn main() -\u003e Result\u003c(), JsValue\u003e {\n    // provide a custom panic hook\n    yew::set_custom_panic_hook(Basic.into_panic_hook());\n    // run the application\n    yew::Renderer::\u003capp::Application\u003e::new().render();\n    Ok(())\n}\n```\n\n## Future improvements\n\nAdditional improvements could be done, like:\n\n* [ ] Call a diagnostic endpoint with the panic information\n* [ ] Create a mode which overlays instead of replaces the HTML body\n* [ ] Allow adding additional, application specific, HTML (like \"click here to report the error\") \n* [ ] For sure some more …\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctron%2Fbrowser-panic-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fctron%2Fbrowser-panic-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctron%2Fbrowser-panic-hook/lists"}