Implement JSG_TRY / JSG_CATCH macros#6021
Merged
harrishancock merged 3 commits intomainfrom Feb 11, 2026
Merged
Conversation
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>
jasnell
approved these changes
Feb 6, 2026
Collaborator
jasnell
left a comment
There was a problem hiding this comment.
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?
Collaborator
Author
Good idea, done. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: