Skip to content
Source
Sign inSign up
TanStack/pacer

fix(async-retryer): preserve non-Error message and cause in normalized errors #223

OpentheBGuy(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/223/headopened

🎯 Changes

  • Fix AsyncRetryer error normalization for non-Error throws.
  • Preserve readable message when a plain object with a string message field is thrown.
  • Attach the original thrown value as Error.cause.
  • Add regression coverage to ensure:
    • message is preserved as "readable error"
    • cause equals the original thrown object

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling during retry operations by better normalizing non-Error thrown values. Error messages from plain objects are now properly preserved, and the original thrown value is accessible via the error cause property. This enables superior error tracking and debugging capabilities.

Review Change Stack

    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      📝 Walkthrough

      Walkthrough

      This PR fixes error normalization in AsyncRetryer.execute(). When a retried function rejects with a non-Error value, it is now converted to an Error whose message is derived from the thrown value's message property or string representation, and the original value is attached as the error's cause for later inspection.

      Changes

      AsyncRetryer Error Normalization

      Layer / File(s)Summary
      Error normalization implementation and validation
      packages/pacer/src/async-retryer.ts, packages/pacer/tests/async-retryer.test.ts
      When handling exceptions in execute, non-Error throws are converted into Error objects with messages derived from a value's message field (if available) or String(error), and the original thrown value is attached as cause. A test validates that plain-object rejections are normalized and the original object is preserved.
      Patch release documentation
      .changeset/big-bikes-wash.md
      Changeset entry documents the @tanstack/pacer patch bump and describes the AsyncRetryer error-normalization behavior change.

      Estimated code review effort

      🎯 2 (Simple) | ⏱️ ~10 minutes

      Poem

      A retryer once threw and caught plain object messiness, Now errors wrap gently, preserving the cause with faithfulness, Where message flows clear and the cause keeps truth, A patch brings precision to async pursuits. 🐰✨

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the primary change: fixing AsyncRetryer error normalization to preserve non-Error message and cause.
      Description check✅ PassedThe PR description follows the template with all required sections completed. Changes are clearly documented, checklist items are checked, release impact is noted, and a changeset was generated.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    2. Pull request overview

      Note

      Copilot was unable to run its full agentic suite in this review.

      This PR improves AsyncRetryer error normalization so that when a non-Error value (e.g., a plain object) is thrown, the normalized Error preserves a readable message and retains the original thrown value via cause.

      Changes:

      • Normalize non-Error thrown values by extracting a message field when present and attaching the original value as Error.cause.
      • Add a regression test ensuring plain-object error message and cause are preserved.
      • Add a changeset to publish the fix as a patch.

      Reviewed changes

      Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

      FileDescription
      packages/pacer/src/async-retryer.tsUpdates error normalization to preserve plain-object messages and attach the original thrown value as cause.
      packages/pacer/tests/async-retryer.test.tsAdds a test covering thrown plain objects and verifying message/cause behavior.
      .changeset/big-bikes-wash.mdDocuments the patch release for the error-normalization fix.

      💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

      @@ -538,4 +538,14 @@
      538538 ) {
      539539 return undefined
      540540 }
      541- lastError = error instanceof Error ? error : new Error(String(error))
      541+ lastError =
      542+ error instanceof Error
      543+ ? error
      544+ : new Error(
      545+ typeof error === 'object' &&
      546+ error !== null &&
      547+ typeof (error as any).message === 'string'
      548+ ? (error as any).message
      549+ : String(error),
      550+ { cause: error },
      551+ )
      copilot-pull-request-reviewer[bot](GitHub profile, opens in a new tab)commented

      The new logic relies on (error as any).message casts. Consider replacing any with a small type guard (e.g., narrow to Record<string, unknown> and check 'message' in error) to keep this path type-safe and easier to maintain.

    3. 🧹 Nitpick comments (1)
      packages/pacer/tests/async-retryer.test.ts (1)

      391-407: 💤 Low value

      Consider adding test coverage for additional edge cases.

      The current test validates the main use case (plain object with string message), and existing tests cover string, null, and undefined. For completeness, you might consider adding tests for:

      • Plain object without a message property (should fall back to String(error))
      • Plain object with non-string message property (e.g., { message: 123 })

      These would ensure the fallback logic is thoroughly validated.

      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@packages/pacer/tests/async-retryer.test.ts` around lines 391 - 407, Add two
      new tests in packages/pacer/tests/async-retryer.test.ts next to the existing
      "should preserve plain object error message and cause" test to cover edge cases:
      (1) a plain object error without a message property (e.g., { code: 'E002' }) and
      assert that AsyncRetryer.execute() produces an Error whose message falls back to
      String(error) and whose cause is the original object; (2) a plain object with a
      non-string message (e.g., { message: 123 }) and assert that execute() converts
      that message to a string for Error.message and sets cause to the original
      object; reference the AsyncRetryer class and its execute() method when locating
      where to add these tests.
      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Nitpick comments:
      In `@packages/pacer/tests/async-retryer.test.ts`:
      - Around line 391-407: Add two new tests in
      packages/pacer/tests/async-retryer.test.ts next to the existing "should preserve
      plain object error message and cause" test to cover edge cases: (1) a plain
      object error without a message property (e.g., { code: 'E002' }) and assert that
      AsyncRetryer.execute() produces an Error whose message falls back to
      String(error) and whose cause is the original object; (2) a plain object with a
      non-string message (e.g., { message: 123 }) and assert that execute() converts
      that message to a string for Error.message and sets cause to the original
      object; reference the AsyncRetryer class and its execute() method when locating
      where to add these tests.

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 60b245f8-b810-48ce-aed3-227146820f8a

      📥 Commits

      Reviewing files that changed from the base of the PR and between a894009100aeb373965d4121eb92a1af634af012 and 4a83cc6429d10055f9141c76611409085f70b928.

      📒 Files selected for processing (3)
      • .changeset/big-bikes-wash.md
      • packages/pacer/src/async-retryer.ts
      • packages/pacer/tests/async-retryer.test.ts

Sign in to join this conversation.