Skip to content

Commit 1f3e2b8

Browse files
committed
Initial commit! 🎉
0 parents  commit 1f3e2b8

File tree

7 files changed

+226
-0
lines changed

7 files changed

+226
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
README.md -diff

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: rust
2+
cache: cargo
3+
4+
rust:
5+
- nightly
6+
7+
script:
8+
- cargo test
9+
- rustup target add wasm32-unknown-unknown --toolchain nightly
10+
- cargo build --target wasm32-unknown-unknown

Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
authors = ["Nick Fitzgerald <[email protected]>"]
3+
categories = ["wasm"]
4+
description = "A panic hook for `wasm32-unknown-unknown` that logs panics to `console.error`"
5+
license = "Apache-2.0/MIT"
6+
name = "console_error_panic_hook"
7+
readme = "./README.md"
8+
repository = "https://github.com/rustwasm/console_error_panic_hook"
9+
version = "0.1.0"
10+
11+
[badges]
12+
travis-ci = { repository = "rustwasm/console_error_panic_hook" }
13+
14+
[dependencies]
15+
cfg-if = "0.1.3"
16+
wasm-bindgen = "0.2.10"

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# console_error_panic_hook
2+
3+
## `console_error_panic_hook`
4+
5+
[![Build Status](https://travis-ci.org/rustwasm/console_error_panic_hook.svg?branch=master)](https://travis-ci.org/rustwasm/console_error_panic_hook)
6+
7+
This crate lets you debug panics on `wasm32-unknown-unknown` by providing a
8+
panic hook that forwards panic messages to
9+
[`console.error`](https://developer.mozilla.org/en-US/docs/Web/API/Console/error).
10+
11+
When an error is reported with `console.error`, browser devtools and node.js
12+
will typically capture a stack trace and display it with the logged error
13+
message.
14+
15+
### Usage
16+
17+
There are two ways to install this panic hook.
18+
19+
First, you can set the hook yourself by calling `std::panic::set_hook` in
20+
some initialization function:
21+
22+
```rust
23+
extern crate console_error_panic_hook;
24+
use std::panic;
25+
26+
fn my_init_function() {
27+
panic::set_hook(Box::new(console_error_panic_hook::hook));
28+
29+
// ...
30+
}
31+
```
32+
33+
Alternatively, use `set_once` on some common code path to ensure that
34+
`set_hook` is called, but only the one time. Under the hood, this uses
35+
`std::sync::Once`.
36+
37+
```rust
38+
extern crate console_error_panic_hook;
39+
40+
struct MyBigThing;
41+
42+
impl MyBigThing {
43+
pub fn new() -> MyBigThing {
44+
console_error_panic_hook::set_once();
45+
46+
MyBigThing
47+
}
48+
}
49+
```
50+
51+
License: Apache-2.0/MIT

src/lib.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//! # `console_error_panic_hook`
2+
//!
3+
//! [![Build Status](https://travis-ci.org/rustwasm/console_error_panic_hook.svg?branch=master)](https://travis-ci.org/rustwasm/console_error_panic_hook)
4+
//!
5+
//! This crate lets you debug panics on `wasm32-unknown-unknown` by providing a
6+
//! panic hook that forwards panic messages to
7+
//! [`console.error`](https://developer.mozilla.org/en-US/docs/Web/API/Console/error).
8+
//!
9+
//! When an error is reported with `console.error`, browser devtools and node.js
10+
//! will typically capture a stack trace and display it with the logged error
11+
//! message.
12+
//!
13+
//! ## Usage
14+
//!
15+
//! There are two ways to install this panic hook.
16+
//!
17+
//! First, you can set the hook yourself by calling `std::panic::set_hook` in
18+
//! some initialization function:
19+
//!
20+
//! ```
21+
//! extern crate console_error_panic_hook;
22+
//! use std::panic;
23+
//!
24+
//! fn my_init_function() {
25+
//! panic::set_hook(Box::new(console_error_panic_hook::hook));
26+
//!
27+
//! // ...
28+
//! }
29+
//! ```
30+
//!
31+
//! Alternatively, use `set_once` on some common code path to ensure that
32+
//! `set_hook` is called, but only the one time. Under the hood, this uses
33+
//! `std::sync::Once`.
34+
//!
35+
//! ```
36+
//! extern crate console_error_panic_hook;
37+
//!
38+
//! struct MyBigThing;
39+
//!
40+
//! impl MyBigThing {
41+
//! pub fn new() -> MyBigThing {
42+
//! console_error_panic_hook::set_once();
43+
//!
44+
//! MyBigThing
45+
//! }
46+
//! }
47+
//! ```
48+
49+
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
50+
51+
#[macro_use]
52+
extern crate cfg_if;
53+
54+
use std::panic;
55+
56+
cfg_if! {
57+
if #[cfg(target_arch = "wasm32")] {
58+
extern crate wasm_bindgen;
59+
use wasm_bindgen::prelude::*;
60+
61+
#[wasm_bindgen]
62+
extern {
63+
#[wasm_bindgen(js_namespace = console)]
64+
fn error(msg: String);
65+
}
66+
67+
fn hook_impl(info: &panic::PanicInfo) {
68+
error(info.to_string());
69+
}
70+
} else {
71+
use std::io::{self, Write};
72+
73+
fn hook_impl(info: &panic::PanicInfo) {
74+
let _ = writeln!(io::stderr(), "{}", info);
75+
}
76+
}
77+
}
78+
79+
/// A panic hook for use with
80+
/// [`std::panic::set_hook`](https://doc.rust-lang.org/nightly/std/panic/fn.set_hook.html)
81+
/// that logs panics into
82+
/// [`console.error`](https://developer.mozilla.org/en-US/docs/Web/API/Console/error).
83+
///
84+
/// On non-wasm targets, prints the panic to `stderr`.
85+
pub fn hook(info: &panic::PanicInfo) {
86+
hook_impl(info);
87+
}
88+
89+
/// Set the `console.error` panic hook the first time this is called. Subsequent
90+
/// invocations do nothing.
91+
pub fn set_once() {
92+
use std::sync::{ONCE_INIT, Once};
93+
static SET_HOOK: Once = ONCE_INIT;
94+
SET_HOOK.call_once(|| {
95+
panic::set_hook(Box::new(hook));
96+
});
97+
}

tests/tests.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
extern crate console_error_panic_hook;
2+
3+
use std::fs::File;
4+
use std::io::Read;
5+
use std::panic;
6+
use std::process::Command;
7+
8+
#[test]
9+
fn cargo_readme_up_to_date() {
10+
println!("Checking that `cargo readme > README.md` is up to date...");
11+
12+
let output = Command::new("cargo")
13+
.arg("readme")
14+
.current_dir(env!("CARGO_MANIFEST_DIR"))
15+
.output()
16+
.expect("should run `cargo readme` OK");
17+
18+
assert!(
19+
output.status.success(),
20+
"Check if you have `cargo-readme` in $PATH"
21+
);
22+
let expected = String::from_utf8_lossy(&output.stdout);
23+
24+
let actual = {
25+
let mut file = File::open(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))
26+
.expect("should open README.md file");
27+
let mut s = String::new();
28+
file.read_to_string(&mut s)
29+
.expect("should read contents of file to string");
30+
s
31+
};
32+
33+
if actual != expected {
34+
panic!("Run `cargo readme > README.md` to update README.md");
35+
}
36+
}
37+
38+
#[test]
39+
fn can_set_as_hook() {
40+
panic::set_hook(Box::new(console_error_panic_hook::hook));
41+
}
42+
43+
#[test]
44+
fn can_set_once() {
45+
for _ in 0..10 {
46+
console_error_panic_hook::set_once();
47+
}
48+
}

0 commit comments

Comments
 (0)