Skip to content

Commit

Permalink
[SKIP CI] improve logging (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhe8x authored Mar 15, 2024
1 parent ef208a5 commit 1739fc6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
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);
});

0 comments on commit 1739fc6

Please sign in to comment.