Skip to content

Commit

Permalink
test: add test fetch_json_error.test.ts, add: 503 handler
Browse files Browse the repository at this point in the history
  • Loading branch information
19Nazar committed Nov 13, 2024
1 parent cd0c6f1 commit d203d42
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
13 changes: 8 additions & 5 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 ([500, 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,16 +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 === 500) {
throw new ProviderError(`Internal server error`, { cause: status });
} else if (status === 408) {
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 }));
});
});

0 comments on commit d203d42

Please sign in to comment.