Skip to content

Conversation

@harrishancock
Copy link
Collaborator

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));
  }

harrishancock and others added 2 commits February 5, 2026 14:46
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>
Replace some manual v8::TryCatch usage with the new JSG_TRY/JSG_CATCH macros
to try them out.

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

@jasnell jasnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know humans aren't likely to read it, but it's good for the LLMs... can you add some details about this to the jsg/README.md?

@harrishancock
Copy link
Collaborator Author

can you add some details about this to the jsg/README.md?

Good idea, done.

@harrishancock harrishancock merged commit 232c276 into main Feb 11, 2026
22 checks passed
@harrishancock harrishancock deleted the harris/2026-02-05-jsg-try-catch branch February 11, 2026 17:21
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