Skip to content

refactor: improve error handling in consumeAsyncIterator function#1639

Merged
dinwwwh merged 1 commit into
middleapi:mainfrom
dinwwwh:refactor/improve-error-handling-in-consumeAsyncIterator
Jul 7, 2026
Merged

refactor: improve error handling in consumeAsyncIterator function#1639
dinwwwh merged 1 commit into
middleapi:mainfrom
dinwwwh:refactor/improve-error-handling-in-consumeAsyncIterator

Conversation

@dinwwwh

@dinwwwh dinwwwh commented Jul 7, 2026

Copy link
Copy Markdown
Member

No description provided.

@vercel

vercel Bot commented Jul 7, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
orpc Ready Ready Preview, Comment Jul 7, 2026 8:36am

@pkg-pr-new

pkg-pr-new Bot commented Jul 7, 2026

Copy link
Copy Markdown
More templates

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@1639

@orpc/bun

npm i https://pkg.pr.new/@orpc/bun@1639

@orpc/client

npm i https://pkg.pr.new/@orpc/client@1639

@orpc/cloudflare

npm i https://pkg.pr.new/@orpc/cloudflare@1639

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@1639

@orpc/experimental-effect

npm i https://pkg.pr.new/@orpc/experimental-effect@1639

@orpc/evlog

npm i https://pkg.pr.new/@orpc/evlog@1639

@orpc/interop

npm i https://pkg.pr.new/@orpc/interop@1639

@orpc/json-schema

npm i https://pkg.pr.new/@orpc/json-schema@1639

@orpc/nest

npm i https://pkg.pr.new/@orpc/nest@1639

@orpc/next

npm i https://pkg.pr.new/@orpc/next@1639

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@1639

@orpc/opentelemetry

npm i https://pkg.pr.new/@orpc/opentelemetry@1639

@orpc/pino

npm i https://pkg.pr.new/@orpc/pino@1639

@orpc/publisher

npm i https://pkg.pr.new/@orpc/publisher@1639

@orpc/ratelimit

npm i https://pkg.pr.new/@orpc/ratelimit@1639

@orpc/server

npm i https://pkg.pr.new/@orpc/server@1639

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@1639

@orpc/tanstack-query

npm i https://pkg.pr.new/@orpc/tanstack-query@1639

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@1639

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@1639

commit: 7dd0bfe

@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Important

This refactor changes the public consumeAsyncIterator contract so iterator errors are silently swallowed when no handler is provided, and leaves the related JSDoc warning stale.

Reviewed changes — removes the rethrow branch in consumeAsyncIterator's catch block so iterator errors are no longer surfaced as unhandled rejections.

  • Remove rethrow in consumeAsyncIterator — errors now pass only to options.onError?.(...); without onError, they are silently swallowed.
  • Delete unhandled-rejection test — the test covering the removed behavior is removed.

⚠️ Iterator errors now swallowed silently when no handler is provided

The replaced catch block deliberately threw the error from a fire-and-forget async IIFE so it landed in the unhandled-rejection channel. That behavior was documented by the @warning JSDoc and verified by the deleted test. The new catch only invokes options.onError?.(...); if the caller supplies neither onError nor onFinish, the error vanishes.

This changes the public API contract of an export used by @orpc/client and makes misconfigured callers harder to debug.

Technical details
# Iterator errors now swallowed silently when no handler is provided

## Affected sites
- `packages/shared/src/iterator.ts:220``catch (error)` now calls only `options.onError?.(...)`; no rethrow or fallback surfacing.
- `packages/shared/src/iterator.ts:187` — JSDoc `@warning` still claims errors are thrown into the unhandled-rejection channel, which is no longer true after this change.
- `packages/shared/src/iterator.test.ts:468-496` — the test that verified the unhandled-rejection path is removed without a replacement.

## Required outcome
- The documented behavior must match the actual behavior. If errors are intentionally swallowed, update or remove the stale `@warning` JSDoc.
- The new contract must be covered by a test so future refactors don't reintroduce or alter it accidentally.

## Suggested approach
- If silent swallowing is intentional: update the JSDoc to say errors are ignored when no `onError`/`onFinish` is provided, and add a test asserting that no unhandled rejection fires in that case.
- If silent swallowing is unintentional: keep the error surfaced. A cleaner alternative to throwing inside the void async IIFE is attaching a rejection handler to the IIFE promise and forwarding to the callbacks, or requiring `onError`/`onFinish` at the type level.

## Open questions for the human
- Is the new silent-swallowing behavior an intentional contract change, or should errors still be surfaced to callers who omit handlers?

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏

@@ -219,15 +219,6 @@ export function consumeAsyncIterator<T, TReturn, TError = ThrowableError>(
}
catch (error) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This catch block now silently swallows iterator errors when neither onError nor onFinish is provided. The previous behavior threw the error into the unhandled-rejection channel so misconfigured callers were at least noisy. If silent swallowing is intentional, add a test that locks in the new contract and update the stale @warning JSDoc at iterator.ts:187.

})
})

it('on error without onError and onFinish', async ({ onTestFinished }) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This test is removed without a replacement that documents the new error-handling contract. Add a test that verifies the expected behavior when no onError/onFinish is provided — either that no unhandled rejection is produced, or that the error is still surfaced somehow.

@dinwwwh dinwwwh merged commit 11f5cd5 into middleapi:main Jul 7, 2026
9 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.

1 participant