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 into
main fromrefs/pull/223/headopened - @@ -538,4 +538,14 @@538538 ) {539539 return undefined540540 }541- lastError = error instanceof Error ? error : new Error(String(error))541+ lastError =542+ error instanceof Error543+ ? error544+ : new Error(545+ typeof error === 'object' &&546+ error !== null &&547+ typeof (error as any).message === 'string'548+ ? (error as any).message549+ : String(error),550+ { cause: error },551+ )packages/pacer/src/async-retryer.ts lines 541 to 551Resolvedby theBGuy(GitHub profile, opens in a new tab)@@ -538,4 +538,14 @@538538 ) {539539 return undefined540540 }541- lastError = error instanceof Error ? error : new Error(String(error))541+ lastError =542+ error instanceof Error543+ ? error544+ : new Error(545+ typeof error === 'object' &&546+ error !== null &&547+ typeof (error as any).message === 'string'548+ ? (error as any).message549+ : String(error),550+ { cause: error },551+ )packages/pacer/tests/async-retryer.test.ts lines 402 to 406Resolvedby theBGuy(GitHub profile, opens in a new tab)@@ -387,3 +387,20 @@387387 expect(onLastError).toHaveBeenCalledTimes(1)388388 expect(onLastError).toHaveBeenCalledWith(error, retryer)389389 })390+391+ it('should preserve plain object error message and cause', async () => {392+ const originalError = { message: 'readable error', code: 'E001' }393+ const mockFn = vi.fn().mockRejectedValue(originalError)394+ const retryer = new AsyncRetryer(mockFn, {395+ maxAttempts: 1,396+ throwOnError: true,397+ })398+399+ try {400+ await retryer.execute()401+ expect.unreachable('Expected execute to throw')402+ } catch (error) {403+ expect(error).toBeInstanceOf(Error)404+ expect((error as Error).message).toBe('readable error')405+ expect((error as Error & { cause?: unknown }).cause).toBe(originalError)406+ }
Sign in to join this conversation.
📝 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'smessageproperty or string representation, and the original value is attached as the error'scausefor later inspection.Changes#
AsyncRetryer Error Normalization
packages/pacer/src/async-retryer.ts,packages/pacer/tests/async-retryer.test.tsexecute, non-Error throws are converted into Error objects with messages derived from a value'smessagefield (if available) orString(error), and the original thrown value is attached ascause. A test validates that plain-object rejections are normalized and the original object is preserved..changeset/big-bikes-wash.md@tanstack/pacerpatch bump and describes the AsyncRetryer error-normalization behavior change.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~10 minutes
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
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