Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support non-200 RPC error #1404

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions packages/providers/src/fetch_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const retryConfig = {
numOfAttempts: RETRY_NUMBER,
timeMultiple: BACKOFF_MULTIPLIER,
retry: (e: ProviderError) => {
if ([503, 408].includes(e.cause)) {
if ([503, 500, 408].includes(e.cause)) {
return true;
}

Expand All @@ -26,7 +26,7 @@ export interface ConnectionInfo {
headers?: { [key: string]: string | number };
}

class ProviderError extends Error {
export class ProviderError extends Error {
cause: number;
constructor(message: string, options: any) {
super(message, options);
Expand Down Expand Up @@ -56,14 +56,19 @@ export async function fetchJsonRpc(url: string, json: JsonRpcRequest, headers: o
});

const { ok, status } = res;
if (!ok) {
throw new ProviderError(await res.text(), { cause: status });
}

if (status === 503) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reason for removing 503 error? It is still the case as I see in the nearcore code. It should be retried as well

throw new ProviderError(`${url} unavailable`, { cause: status });
if (status === 500) {
throw new ProviderError(`Internal server error`, { cause: status });
} else if (status === 408) {
throw new ProviderError('Unused connection', { cause: status });
throw new ProviderError('Timeout error', { cause: status });
} else if (status === 400) {
throw new ProviderError('Request validation error', { cause: status });
} else if (status === 503) {
throw new ProviderError(`${url} unavailable`, { cause: status });
}

if (!ok) {
throw new ProviderError(await res.text(), { cause: status });
}

return res;
Expand Down
74 changes: 74 additions & 0 deletions packages/providers/test/fetch_json_error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, expect, test, jest, afterEach } from '@jest/globals';
import { fetchJsonRpc } from '../src/fetch_json';
import unfetch from 'isomorphic-unfetch';
import { ProviderError } from '../src/fetch_json';

jest.mock('isomorphic-unfetch');

describe('fetchJsonError', () => {
const RPC_URL = 'https://rpc.testnet.near.org';
const statusRequest = {
jsonrpc: '2.0',
id: 'dontcare',
method: 'status',
params: [],
};

afterEach(() => {
jest.clearAllMocks();
});

test('handles 500 Internal Server Error', async () => {
(unfetch as jest.Mock).mockReturnValue({
ok: false,
status: 500,
text: '',
json: {},
});

// @ts-expect-error test input
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined))
.rejects
.toThrowError(new ProviderError('Internal server error', { cause: 500 }));
});
test('handles 408 Timeout Error', async () => {
(unfetch as jest.Mock).mockReturnValue({
ok: false,
status: 408,
text: '',
json: {},
});
// @ts-expect-error test input
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined))
.rejects
.toThrowError(new ProviderError('Timeout error', { cause: 408 }));
});
// });

test('handles 400 Request Validation Error', async () => {
(unfetch as jest.Mock).mockReturnValue({
ok: false,
status: 400,
text: '',
json: {},
});
// @ts-expect-error test input
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined))
.rejects
.toThrowError(new ProviderError('Request validation error', { cause: 400 }));
});

test('handles 503 Service Unavailable', async () => {
(unfetch as jest.Mock).mockReturnValue({
ok: false,
status: 503,
text: '',
json: {},
});

// @ts-expect-error test input
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined))
.rejects
.toThrowError(new ProviderError(`${RPC_URL} unavailable`, { cause: 503 }));
});
});
Loading