Skip to content
Commit Detail

Commit e5651f3

Author
Harris Hancock <harris@cloudflare.com> 2026-02-05 14:34:15 +0000
Parents
9339e1c
Tree
af2d1fb
Implement JSG_TRY / JSG_CATCH macros

Add new macros for exception handling in JSG code that automatically convert
both JavaScript exceptions (JsExceptionThrown) and KJ exceptions (kj::Exception)
to jsg::Value. This enables cleaner exception handling syntax and prepares for
future coroutine support with co_await on jsg::Promise.

The implementation uses a JsgCatchScope helper class that sets up a v8::TryCatch
on construction and converts caught exceptions via catchException(). The macros
use the "goto-dead-else-branch" pattern from KJ_TRY/KJ_CATCH for clean syntax.

Usage:
  JSG_TRY(js) {
    someCodeThatMightThrow();
  } JSG_CATCH(exception) {
    return js.rejectedPromise<void>(kj::mv(exception));
  }

Co-authored-by: Claude <noreply@anthropic.com>

Files changed

3 files changed~3 modified