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

improve logging #268

Merged
merged 1 commit into from
Mar 15, 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
6 changes: 4 additions & 2 deletions packages/network-support/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { OrderManager } from './orderManager';
import { customFetch, generateUniqueId } from './utils';
import { customFetch, generateUniqueId, Logger } from './utils';
import { ChannelState, OrderType } from './types';
import { ScoreType } from './scoreManager';
import { Base64 } from 'js-base64';
Expand Down Expand Up @@ -32,7 +32,8 @@ export class FetchError extends Error {

export function createFetch(
orderManager: OrderManager,
maxRetries = 5
maxRetries = 5,
logger?: Logger
): (init: RequestInit) => Promise<Response> {
let retries = 0;
return async function fetch(init: RequestInit): Promise<Response> {
Expand Down Expand Up @@ -84,6 +85,7 @@ export function createFetch(
text: () => undefined,
} as unknown as Response;
} catch (e) {
logger?.warn(e);
if (retries < maxRetries) {
orderManager.updateScore(runner, ScoreType.RPC);
retries += 1;
Expand Down
6 changes: 5 additions & 1 deletion packages/network-support/src/orderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ export class OrderManager {
return [body, state, ''];
}
default:
throw new Error('invalid X-Indexer-Response-Format');
if (typeof payload === 'object' && (payload as any).code) {
throw new Error(JSON.stringify(payload));
} else {
throw new Error('invalid X-Indexer-Response-Format');
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions packages/network-support/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ describe('eth provider', () => {
logger: mockLogger,
responseFormat: ResponseFormat.Wrapped,
});
const fetch = createFetch(orderManager);
const fetch = createFetch(orderManager, 3, mockLogger);

const res = await fetch({
body: JSON.stringify({ jsonrpc: '2.0', method: 'eth_blockNumber', params: [], id: 1 }),
headers: { 'Content-Type': 'application/json' },
method: 'post',
});
expect(res).toBeTruthy();
expect(await res.json()).toBeTruthy();
for (let i = 0; i < 10; i++) {
const res = await fetch({
body: JSON.stringify({ jsonrpc: '2.0', method: 'eth_blockNumber', params: [], id: 1 }),
headers: { 'Content-Type': 'application/json' },
method: 'post',
});
expect(res).toBeTruthy();
expect(await res.json()).toBeTruthy();
}
}, 30000);
});
Loading