Skip to content

Commit

Permalink
feat: make BaseError#walk tolerant to an undefined cause (#2954)
Browse files Browse the repository at this point in the history
* Make BaseError#walk tolerant to an undefined cause

* Create khaki-queens-explain.md

---------

Co-authored-by: jxom <j@wevm.dev>
  • Loading branch information
alcuadrado and jxom authored Nov 3, 2024
1 parent 9f7a8f3 commit 4f931a5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/khaki-queens-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Make `BaseError#walk` tolerant to an `undefined` cause.
9 changes: 9 additions & 0 deletions src/errors/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ test('walk: predicate fn (no match)', () => {
expect(err.walk((err) => err instanceof FooError)).toBeNull()
})

test('walk: undefined cause', () => {
const withCauseUndefined = new Error('Cuase undefined', { cause: undefined })

const err = new BaseError('test1', {
cause: withCauseUndefined,
})
expect(err.walk()).toBe(withCauseUndefined)
})

test('setErrorConfig', () => {
class FooError extends BaseError {
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion src/errors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function walk(
fn?: ((err: unknown) => boolean) | undefined,
): unknown {
if (fn?.(err)) return err
if (err && typeof err === 'object' && 'cause' in err)
if (err && typeof err === 'object' && 'cause' in err && err.cause !== undefined)
return walk(err.cause, fn)
return fn ? null : err
}

0 comments on commit 4f931a5

Please sign in to comment.