Skip to content

fix: convert Go runtime panics to goja GoError exceptions#23

Merged
danielkov merged 4 commits into
mainfrom
fix/recover-go-runtime-panics-for-js-trycatch
Mar 4, 2026
Merged

fix: convert Go runtime panics to goja GoError exceptions#23
danielkov merged 4 commits into
mainfrom
fix/recover-go-runtime-panics-for-js-trycatch

Conversation

@danielkov
Copy link
Copy Markdown
Contributor

Summary

  • Go runtime panics (e.g. nil-pointer dereference) from native functions now get caught by JS try/catch blocks
  • Previously, these panics bypassed JS error handling entirely because goja's exceptionFromValue only recognises its own types — raw Go panics fell through to a default case that re-panicked
  • Adds a deferred recover in the wrappedFn closure (the Go→JS native function bridge) that converts Go runtime panics to GoError exceptions

This fixes an intermittent CI failure in openapi-generation where Ruby SDK generation crashed with:

templateUsageImports .Local.Operation: runtime error: invalid memory address or nil pointer dereference

The Ruby template already had try/catch around the offending call, but the Go panic bypassed it.

✻ Clauded...

danielkov and others added 3 commits March 4, 2026 15:43
…unction bridge

Go runtime panics (e.g. nil-pointer dereference, index out of range) from
native functions bypass JS try/catch blocks because goja's exceptionFromValue
only recognises its own types — raw Go panics fall through to a default case
that re-panics, skipping the JS catch handler entirely.

This adds a deferred recover in the wrappedFn closure that bridges Go native
functions to JS. It catches Go runtime panics and re-panics with a GoError
exception that goja can route through its try/catch mechanism. Goja-compatible
panic values (goja.Value, *goja.Exception) are re-thrown unchanged.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add PanicError type that carries both the Go stack trace (from the panic
site) and the JS call stack (from the goja VM), so downstream consumers
can extract full context via errors.As.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@danielkov danielkov merged commit 9093a38 into main Mar 4, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants