console_error_panic_hook
A panic hook for wasm32-unknown-unknown that forwards Rust panic messages to console.error
Repository Health
Technical Analysis
When a Rust panic occurs in a wasm32-unknown-unknown WebAssembly build without this crate installed, the browser or Node.js console typically shows only an opaque RuntimeError: Unreachable executed, with no indication of what actually went wrong in the Rust source. console_error_panic_hook replaces the default panic behavior with one that logs the real panic message (and, where available, a stack trace) via console.error, so browser devtools and Node.js can surface it properly.
Installation is a single call — either panic::set_hook(Box::new(console_error_panic_hook::hook)) in an init function, or console_error_panic_hook::set_once() guarded by std::sync::Once on a common code path so the hook is only installed once regardless of how many times the initializer runs.
What You Get
- A
hookfunction to pass directly tostd::panic::set_hookfor readable panic messages in WASM builds - A
set_once()convenience function (backed bystd::sync::Once) to install the hook exactly once from any repeatedly-called initializer - Panic messages routed through
console.error, which browser devtools and Node.js both capture with an accompanying stack trace - A no-op / pass-through implementation on non-wasm32 targets via
cfg_if, so the same code compiles cleanly for native testing
Common Use Cases
- Debugging Rust-compiled-to-WebAssembly applications where panics otherwise surface only as an opaque ‘unreachable executed’ trap
- Standard boilerplate added to the entry point of
wasm-bindgen-based WASM libraries and applications during development - Improving crash diagnostics for WASM modules run in Node.js server-side or tooling contexts
- Pairing with
wasm-pack-built crates to give contributors readable error output during local development
Under The Hood
Architecture: The entire implementation lives in a single src/lib.rs. It uses the cfg_if! macro to branch on target_arch = "wasm32": on wasm32 it pulls in wasm_bindgen to call the browser’s console.error JS function with the formatted panic message and location; on other targets it compiles to effectively nothing (or a fallback), so the same crate can be a dependency in cross-platform test suites without pulling in wasm-only code paths. set_once() wraps panic::set_hook in a std::sync::Once so repeated calls (e.g. from a constructor called many times) still only register the hook a single time.
Tech Stack: A minimal Rust crate whose only two dependencies are cfg-if (for the target-arch branching) and wasm-bindgen (for the JS interop that calls console.error). No build tooling beyond standard cargo — this is a leaf dependency meant to be pulled into other wasm-bindgen projects.
Code Quality: The crate ships doc-tested usage examples directly in src/lib.rs (both the set_hook and set_once patterns), plus a tests/ directory, keeping the documented API and tested behavior in sync. At ~6.5KB of Rust source, the entire implementation is small enough to audit in minutes, and its single-purpose scope has kept it stable with no need for frequent updates (last commit several years prior to this analysis) despite continued heavy download volume.
API Design: The two-line integration — either panic::set_hook(Box::new(console_error_panic_hook::hook)) or console_error_panic_hook::set_once() — requires no configuration and composes with any existing panic-hook chain via standard std::panic APIs, making it one of the lowest-friction debugging aids in the wasm-bindgen ecosystem.
Used by 5 apps in this directory
hoodik
File Storage · Security
Self-hosted, end-to-end encrypted cloud storage with browser-based encryption and S3-compatible storage support
Joplin
Note Taking
The privacy-first, open-source note-taking app with end-to-end encrypted sync, AI assistance, and a powerful plugin ecosystem across every platform.
liteparse
Developer Tools
A fast, lightweight, open-source document parser that extracts spatial text, bounding boxes, and Markdown from PDFs and Office files — entirely on your machine.
OpenPencil
AI Design Tools
An open-source, AI-native vector design tool with concurrent agent teams, design-as-code, a built-in MCP server, and multi-model intelligence — a distinct project from the similarly-named Figma-file-reading "open-pencil" editor.
WrenAI
Analytics · AI Agents · Data Engineering
Open-source GenBI engine that lets AI agents turn natural-language questions into governed SQL, charts, and shareable dashboards across 20+ data sources — no vendor lock-in, no black-box prompts.