swc-project/swc · error

failed to handle with unknown panic message

Error message

failed to handle with unknown panic message

What it means

The catch_unwind wrapper in binding_html_node/src/util.rs could not downcast the panic payload to String or &str, so it reports 'failed to handle with unknown panic message'. This happens when code panics with a non-string payload - e.g. unwrap() on a Result/Option whose error type is not &str/String (it panics with the Debug value), assert failures on integers, or panic_any with custom types. The original cause is therefore not visible in the error text.

Source

Thrown at bindings/binding_html_node/src/util.rs:31

            skip_filename: false,
            ..Default::default()
        },
        |handler| {
            //
            let result =
                std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| op(&cm, handler)));

            let p = match result {
                Ok(v) => return v,
                Err(v) => v,
            };

            if let Some(s) = p.downcast_ref::<String>() {
                Err(anyhow!("failed to handle: {s}"))
            } else if let Some(s) = p.downcast_ref::<&str>() {
                Err(anyhow!("failed to handle: {s}"))
            } else {
                Err(anyhow!("failed to handle with unknown panic message"))
            }
        },
    )
    .map_err(|e| e.to_pretty_error())
}

View on GitHub (pinned to d7d7434666)

Solutions

  1. Reproduce outside the binding (swc CLI or a small Rust harness with backtraces: RUST_BACKTRACE=1) to recover the real panic payload and location
  2. Minimize the input by bisection even without a message
  3. Upgrade @swc/html - the fix usually lands upstream
  4. Report to swc with the repro; unknown-payload panics are always bugs worth filing
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const out = minify(html, opts);
} catch (e) {
  if (String(e).includes('unknown panic message')) {
    // payload had no message: bisect the input to find the trigger
    failures.push({ html: file, kind: 'unknown-panic' });
    continue;
  }
  throw e;
}

Prevention

When it happens

Trigger: An .unwrap()/.expect() failure deep in swc_html_* reached through @swc/html napi APIs: the payload is the Debug-formatted error value, not a string. Typical for internal invariant violations on malformed HTML where the code panics instead of returning Err.

Common situations: Debugging blind: the message gives no cause; usually appears with edge-case HTML or version regressions where an internal Result was unwrapped.

Related errors


AI-assisted analysis of swc-project/swc@d7d7434666 (2026-08-16). Data as JSON: /api/errors/69f6614d9c8a0017. Report an issue: GitHub.