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

refactor: try error #290

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion packages/network-support/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.6] 2024-05-20

## [1.2.4] 2024-05-13

## [1.2.3] 2024-05-12
Expand Down Expand Up @@ -35,7 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- It's a internal library.

[unreleased]: https://github.com/subquery/network-support/compare/v1.2.4...HEAD
[unreleased]: https://github.com/subquery/network-support/compare/v1.2.6...HEAD
[1.2.6]: https://github.com/subquery/network-support/releases/tag/v1.2.6
[1.2.4]: https://github.com/subquery/network-support/releases/tag/v1.2.4
[1.2.3]: https://github.com/subquery/network-support/releases/tag/v1.2.3
[1.2.2]: https://github.com/subquery/network-support/releases/tag/v1.2.2
Expand Down
2 changes: 1 addition & 1 deletion packages/network-support/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@subql/network-support",
"version": "1.2.4",
"version": "1.2.6",
"main": "dist/index.js",
"author": "SubQuery Pte Limited",
"license": "Apache-2.0",
Expand Down
16 changes: 12 additions & 4 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, Logger } from './utils';
import { customFetch, generateUniqueId, Logger, safeJSONParse } from './utils';
import { OrderType } from './types';
import { ScoreType } from './scoreManager';
import { Base64 } from 'js-base64';
Expand Down Expand Up @@ -38,6 +38,7 @@ export function createFetch(
): (init: RequestInit) => Promise<Response> {
let retries = 0;
let triedFallback = false;
let errorMsg = '';
return async function fetch(init: RequestInit): Promise<Response> {
const requestId = generateUniqueId();
const requestResult: () => Promise<Response> = async () => {
Expand All @@ -60,7 +61,10 @@ export function createFetch(
};
logger?.warn(`fallback to ${orderManager.fallbackServiceUrl}`);
} else {
throw new FetchError(`no available order`, 'sqn');
throw new FetchError(
`no available order. retries: ${retries}.${errorMsg ? ' error: ' + errorMsg : ''}`,
'sqn'
);
}
}
const { url, headers, type, runner, channelId } = requestParams;
Expand Down Expand Up @@ -109,12 +113,16 @@ export function createFetch(
} as unknown as Response;
} catch (e) {
logger?.warn(e);
errorMsg = (e as Error)?.message || '';
if (retries < maxRetries || (orderManager.fallbackServiceUrl && !triedFallback)) {
orderManager.updateScore(runner, ScoreType.RPC);
const errorObj = safeJSONParse(errorMsg);
if (!(errorObj?.code === 1140 && errorObj?.error === 'Invalid request')) {
orderManager.updateScore(runner, ScoreType.RPC);
}
retries += 1;
return requestResult();
}
throw new FetchError(`reach max retries`, 'SQN');
throw new FetchError(`reach max retries.${errorMsg ? ' error: ' + errorMsg : ''}`, 'SQN');
}
};

Expand Down
10 changes: 10 additions & 0 deletions packages/network-support/src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: Apache-2.0

export function safeJSONParse(json: string) {
try {
return JSON.parse(json);
} catch (e) {
return undefined;
}
}
1 change: 1 addition & 0 deletions packages/network-support/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './query';
export * from './store';
export * from './auth';
export * from './uniqueId';
export * from './common';