Skip to content

Commit

Permalink
feat: implement custom bug report forms (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau authored Apr 15, 2024
1 parent 803012e commit 93b3c6f
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 228 deletions.
34 changes: 33 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
/target
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
/Cargo.lock

# Generated by Cargo
# will have compiled files and executables
/target/

# Generated by Trunk
dist/

# These are backup files generated by rustfmt
**/*.rs.bk

# Some people use VSCode
/.vscode/

# Some people use IntelliJ with Rust
/.idea/
*.iml

# Some people use pre-commit
.pre-commit-config.yaml
.pre-commit-config.yml

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# Some people have Apple
.DS_Store

# Generated on wasm-pack failure
**/unsupported.js
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
# Changelog

<!-- Instructions
This changelog follows the patterns described here: <https://keepachangelog.com/en/1.0.0/>.
Subheadings to categorize changes are `added, changed, deprecated, removed, fixed, security`.
-->

## Unreleased

## 0.2.0

### added

- `FORM_TEXTAREA_ID` is now public, which has the element ID of the stack trace text area.
- `FORM_SUBMIT_ID` is now public, which has the element ID of the submit button.

### changed

- `set_hook_with` now takes the additional argument of an HTML form structure. The previous, default form can still be used with `web_panic_report::set_default_hook_with`, however requires the new (default) cargo feature: `default-form`.

## 0.1.1

### changed
Expand Down
197 changes: 0 additions & 197 deletions Cargo.lock

This file was deleted.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "web_panic_report"
description = "A panic hook which replaces an HTML element with a bug report form."
version = "0.1.1"
version = "0.2.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/vectorgameexperts/web_panic_report"
repository = "https://github.com/loopystudios/web_panic_report"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
Expand All @@ -20,3 +20,7 @@ web-sys = { version = "0.3", features = [

[dev-dependencies]
wasm-bindgen-test = "0.3.42"

[features]
default = ["default-form"]
default-form = []
10 changes: 6 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

[![Discord](https://img.shields.io/discord/913957940560531456.svg?label=Loopy&logo=discord&logoColor=ffffff&color=ffffff&labelColor=000000)](https://discord.gg/zrjnQzdjCB)
[![MIT/Apache 2.0](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)](#license)
[![Build status](https://github.com/vectorgameexperts/web_panic_report/workflows/CI/badge.svg)](https://github.com/vectorgameexperts/web_panic_report/actions)
[![Dependency status](https://deps.rs/repo/github/vectorgameexperts/web_panic_report/status.svg)](https://deps.rs/repo/github/vectorgameexperts/web_panic_report)
[![Build status](https://github.com/loopystudios/web_panic_report/workflows/CI/badge.svg)](https://github.com/loopystudios/web_panic_report/actions)
[![dependency status](https://deps.rs/repo/github/loopystudios/web_panic_report/status.svg)](https://deps.rs/repo/github/loopystudios/web_panic_report)
[![Crates.io](https://img.shields.io/crates/v/web_panic_report.svg)](https://crates.io/crates/web_panic_report)
[![Docs](https://img.shields.io/docsrs/web_panic_report)](https://docs.rs/web_panic_report)

Expand All @@ -28,7 +28,7 @@ cargo install wasm-server-runner
WASM_SERVER_RUNNER_CUSTOM_INDEX_HTML=examples/index.html cargo run --target wasm32-unknown-unknown --example simple
```

There is also a web demo [available here](https://vectorgameexperts.github.io/web_panic_report).
There is also a web demo [available here](https://loopystudios.github.io/web_panic_report).

![Demo](image.png)

Expand Down Expand Up @@ -59,13 +59,15 @@ Then, set the panic hook at the beginning of your program on web.
```rust
fn main() {
#[cfg(target_arch = "wasm32")]
web_panic_report::set_hook_with("my-container", |panic_info| {
web_panic_report::set_default_hook_with("my-container", |panic_info| {
// Send the panic info to your backend here.
// This is triggered when the user clicks "Send Report"
});
}
```

You can also use a custom bug report form. See the [custom example](examples/custom.rs).

## Alternatives

- [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) - Only outputs stack trace to the console.
Expand Down
28 changes: 28 additions & 0 deletions examples/custom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// This is called from a button on the web example to allow the user to trigger a panic.
#[no_mangle]
pub extern "C" fn trigger_panic() {
panic!("You triggered the panic!");
}

fn main() {
let my_form = format!(
r#"
<div style="display: flex; flex-direction: column; width: 100%; height: 100%; background-color: white; color: red">
This is a custom panic form! The stack trace is hidden from users.
<textarea readonly id="{}" style="display: none;"></textarea>
<br />
<input id="{}" type="submit" value="See Panic Info" style="width: fit-content;" />
</div>
"#,
web_panic_report::FORM_TEXTAREA_ID,
web_panic_report::FORM_SUBMIT_ID
);

// Set the panic hook at the beginning of your program
web_panic_report::set_hook_with("test-container", my_form, |panic_info| {
web_sys::window()
.unwrap()
.alert_with_message(&panic_info.display.to_string())
.unwrap();
});
}
23 changes: 8 additions & 15 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
use wasm_bindgen::prelude::*;

/// Nothing special here, we just want to call the Window `alert` function in our custom callback.
/// This bindgen gives us access to that.
#[wasm_bindgen]
extern "C" {
/// The Window `alert` function.
fn alert(s: &str);
}

/// This is called from a button on the web example to allow the user to trigger a panic.
#[no_mangle]
pub extern "C" fn trigger_panic() {
Expand All @@ -16,10 +6,13 @@ pub extern "C" fn trigger_panic() {

fn main() {
// Set the panic hook at the beginning of your program
web_panic_report::set_hook_with("test-container", |panic_info| {
alert(&format!(
"This is a custom callback!\n\n{}",
panic_info.display
));
web_panic_report::set_default_hook_with("test-container", |panic_info| {
web_sys::window()
.unwrap()
.alert_with_message(&format!(
"This is a custom callback!\n\n{}",
panic_info.display
))
.unwrap();
});
}
File renamed without changes.
19 changes: 19 additions & 0 deletions src/default_form/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::WasmPanicInfo;

/// The default form HTML.
const FORM_HTML: &str = include_str!("form.html");

/// Set the panic hook, with a default form.
///
/// # Params
/// `container_id`: The ID of the HTML element that will be unmounted in favor of the form.\
/// `submit_callback`: The closure that will run when the user hits the send report button.
///
/// # Panics
/// This will panic (ironically) if the panic occurs in a headless environment.
pub fn set_default_hook_with<F>(container_id: impl Into<String>, submit_callback: F)
where
F: Fn(&WasmPanicInfo) + Send + Sync + 'static,
{
crate::set_hook_with(container_id, FORM_HTML, submit_callback)
}
Loading

0 comments on commit 93b3c6f

Please sign in to comment.