Skip to content

Commit

Permalink
feat: add cause in HttpRequestError
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Jun 28, 2024
1 parent 62b7a9d commit 95753c1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-numbers-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added `cause` in `HttpRequestError`.
13 changes: 3 additions & 10 deletions src/errors/base.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { getVersion } from './utils.js'

type BaseErrorParameters = {
cause?: BaseError | Error | undefined
details?: string | undefined
docsBaseUrl?: string | undefined
docsPath?: string | undefined
docsSlug?: string | undefined
metaMessages?: string[] | undefined
} & (
| {
cause?: undefined
details?: string | undefined
}
| {
cause: BaseError | Error | undefined
details?: undefined
}
)
}

export type BaseErrorType = BaseError & { name: 'ViemError' }
export class BaseError extends Error {
Expand Down
3 changes: 3 additions & 0 deletions src/errors/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ export class HttpRequestError extends BaseError {

constructor({
body,
cause,
details,
headers,
status,
url,
}: {
body?: { [x: string]: unknown } | { [y: string]: unknown }[] | undefined
cause?: Error | undefined
details?: string | undefined
headers?: Headers | undefined
status?: number | undefined
url: string
}) {
super('HTTP request failed.', {
cause,
details,
metaMessages: [
status && `Status: ${status}`,
Expand Down
33 changes: 27 additions & 6 deletions src/utils/rpc/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,27 @@ describe('request', () => {
)
})

test('fetch error', async () => {
vi.stubGlobal('fetch', () => {
throw new Error('foo', { cause: new Error('bar') })
})

const client = getHttpRpcClient(anvilMainnet.rpcUrl.http)

try {
await client.request({
body: {
method: 'eth_getBlockByNumber',
params: [numberToHex(anvilMainnet.forkBlockNumber), false],
},
})
} catch (error) {
expect((error as Error).cause).toMatchInlineSnapshot('[Error: foo]')
}

vi.unstubAllGlobals()
})

// TODO: This is flaky.
test.skip('timeout', async () => {
const client = getHttpRpcClient(anvilMainnet.rpcUrl.http)
Expand Down Expand Up @@ -371,12 +392,12 @@ describe('http (batch)', () => {
).toMatchInlineSnapshot(`
[
{
"id": 89,
"id": 91,
"jsonrpc": "2.0",
"result": "anvil/v0.2.0",
},
{
"id": 90,
"id": 92,
"jsonrpc": "2.0",
"result": "anvil/v0.2.0",
},
Expand All @@ -397,7 +418,7 @@ describe('http (batch)', () => {
).toMatchInlineSnapshot(`
[
{
"id": 92,
"id": 94,
"jsonrpc": "2.0",
"result": "anvil/v0.2.0",
},
Expand All @@ -406,7 +427,7 @@ describe('http (batch)', () => {
"code": -32602,
"message": "Odd number of digits",
},
"id": 93,
"id": 95,
"jsonrpc": "2.0",
},
]
Expand All @@ -423,7 +444,7 @@ describe('http (batch)', () => {
).toMatchInlineSnapshot(`
[
{
"id": 95,
"id": 97,
"jsonrpc": "2.0",
"result": "anvil/v0.2.0",
},
Expand All @@ -432,7 +453,7 @@ describe('http (batch)', () => {
"code": -32601,
"message": "Method not found",
},
"id": 96,
"id": 98,
"jsonrpc": "2.0",
},
]
Expand Down
2 changes: 1 addition & 1 deletion src/utils/rpc/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function getHttpRpcClient(
if (err instanceof TimeoutError) throw err
throw new HttpRequestError({
body,
details: (err as Error).message,
cause: err as Error,
url,
})
}
Expand Down

0 comments on commit 95753c1

Please sign in to comment.