Add ctx.uncaught for emitting uncaught errors, particularly in streams#335
Open
Add ctx.uncaught for emitting uncaught errors, particularly in streams#335
ctx.uncaught for emitting uncaught errors, particularly in streams#335Conversation
| cancel: (message?: string) => ErrResult<Static<typeof CancelErrorSchema>>; | ||
| /** | ||
| * This emits an uncaught error in the same way that throwing an error in a handler | ||
| * would. You should minimize the amount of work you do after calling this function |
Contributor
There was a problem hiding this comment.
Maybe we can create an eslint rule for this
jackyzha0
approved these changes
Jul 15, 2025
| /** | ||
| * This emits an uncaught error in the same way that throwing an error in a handler | ||
| * would. You should minimize the amount of work you do after calling this function | ||
| * as this will start a cleanup of the entire procedure call. |
Member
There was a problem hiding this comment.
maybe clarify this ends the readable/writables
Comment on lines
+48
to
+49
| * You'll typically want to use this for streaming procedures, as in e.g. an RPC | ||
| * you can just throw instead. |
Member
There was a problem hiding this comment.
Suggested change
| * You'll typically want to use this for streaming procedures, as in e.g. an RPC | |
| * you can just throw instead. | |
| * You'll typically want to use this for server-sent stream procedures like subscriptions or streams which may handle things inside closures where throwing will not be caught by River's procedure uncaught handler. |
| * You'll typically want to use this for streaming procedures, as in e.g. an RPC | ||
| * you can just throw instead. | ||
| */ | ||
| uncaught: (err?: unknown) => ErrResult<Static<typeof UncaughtErrorSchema>>; |
Member
There was a problem hiding this comment.
what does it mean to throw an uncaught with no err?
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.
Why
Fixes #333
In streaming procedures, you often need to push an uncaught error into the stream outside of the scope of the handler. This is tricky since all you're left with doing is manually returning an uncaught error rather than using any of the machinery River has built in.
What changed
Added
ctx.uncaughtwhich works a lot likectx.cancelexcept that it usesUNCAUGHT_ERRORrather than the cancel code. It uses the exact same mechanism as the normal procedure error handler and yields you an error result that you can return if needed. It accepts anything as an argument so that you can use it ergonomically in a try catch.Versioning