Skip to content

Commit

Permalink
feat: flag 403 as non-deterministic error for fallback Transport
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Apr 26, 2023
1 parent e79d7bc commit 6f8151c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-apes-reply.md
@@ -0,0 +1,5 @@
---
"viem": patch
---

Flagged 403 as non-deterministic error for `fallback` Transport.
33 changes: 33 additions & 0 deletions src/utils/buildRequest.test.ts
Expand Up @@ -646,6 +646,31 @@ describe('behavior', () => {
expect(retryCount).toBe(3)
})

test('non-deterministic HttpRequestError (403)', async () => {
let retryCount = -1
const server = await createHttpServer((_req, res) => {
retryCount++
res.writeHead(403, {
'Content-Type': 'application/json',
})
res.end(JSON.stringify({}))
})

await expect(() =>
buildRequest(request(server.url))({ method: 'eth_blockNumber' }),
).rejects.toThrowErrorMatchingInlineSnapshot(`
"HTTP request failed.
Status: 403
URL: http://localhost
Request body: {\\"method\\":\\"eth_blockNumber\\"}
Details: Forbidden
Version: viem@1.0.2"
`)
expect(retryCount).toBe(3)
})

test('non-deterministic HttpRequestError (408)', async () => {
let retryCount = -1
const server = await createHttpServer((_req, res) => {
Expand Down Expand Up @@ -814,6 +839,14 @@ describe('isDeterministicError', () => {
).toBe(false)
})

test('HttpRequestError (403)', () => {
expect(
isDeterministicError(
new HttpRequestError({ body: {}, details: '', status: 403, url: '' }),
),
).toBe(false)
})

test('HttpRequestError (408)', () => {
expect(
isDeterministicError(
Expand Down
1 change: 1 addition & 0 deletions src/utils/buildRequest.ts
Expand Up @@ -37,6 +37,7 @@ export const isDeterministicError = (error: Error) => {
)
if (error instanceof HttpRequestError && error.status)
return (
error.status !== 403 &&
error.status !== 408 &&
error.status !== 413 &&
error.status !== 429 &&
Expand Down

1 comment on commit 6f8151c

@vercel
Copy link

@vercel vercel bot commented on 6f8151c Apr 26, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.