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 api error handling #1576

Merged
merged 1 commit into from
Mar 22, 2023
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
34 changes: 34 additions & 0 deletions packages/node/src/indexer/apiPromise.connection.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is an ApiPromiseConnection class in this PR: #1551
I think it would make sense to merge these functions into that class.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2020-2022 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { getLogger } from '@subql/node-core';

const logger = getLogger('connection');

export class ApiPromiseConnection {
static handleError(e: Error): Error {
let formatted_error: Error;
if (e.message.startsWith(`No response received from RPC endpoint in`)) {
formatted_error = this.handleTimeoutError(e);
} else if (e.message.startsWith(`disconnected from `)) {
formatted_error = this.handleDisconnectionError(e);
} else {
formatted_error = e;
}
return formatted_error;
}

static handleTimeoutError(e: Error): Error {
const formatted_error = new Error();
formatted_error.name = 'TimeoutError';
formatted_error.message = e.message;
return formatted_error;
}

static handleDisconnectionError(e: Error): Error {
const formatted_error = new Error();
formatted_error.name = 'ConnectionError';
formatted_error.message = e.message;
return formatted_error;
}
}
9 changes: 5 additions & 4 deletions packages/node/src/utils/substrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '@subql/types';
import { last, merge, range } from 'lodash';
import { SubqlProjectBlockFilter } from '../configure/SubqueryProject';
import { ApiPromiseConnection } from '../indexer/apiPromise.connection';
import { BlockContent } from '../indexer/types';
const logger = getLogger('fetch');
const INTERVAL_THRESHOLD = BN_THOUSAND.div(BN_TWO);
Expand Down Expand Up @@ -255,12 +256,12 @@ export async function getBlockByHeight(
): Promise<SignedBlock> {
const blockHash = await api.rpc.chain.getBlockHash(height).catch((e) => {
logger.error(`failed to fetch BlockHash ${height}`);
throw e;
throw ApiPromiseConnection.handleError(e);
});

const block = await api.rpc.chain.getBlock(blockHash).catch((e) => {
logger.error(`failed to fetch Block ${blockHash}`);
throw e;
throw ApiPromiseConnection.handleError(e);
});
// validate block is valid
if (block.block.header.hash.toHex() !== blockHash.toHex()) {
Expand Down Expand Up @@ -300,7 +301,7 @@ export async function fetchEventsRange(
hashs.map((hash) =>
api.query.system.events.at(hash).catch((e) => {
logger.error(`failed to fetch events at block ${hash}`);
throw e;
throw ApiPromiseConnection.handleError(e);
}),
),
);
Expand All @@ -314,7 +315,7 @@ export async function fetchRuntimeVersionRange(
hashs.map((hash) =>
api.rpc.state.getRuntimeVersion(hash).catch((e) => {
logger.error(`failed to fetch RuntimeVersion at block ${hash}`);
throw e;
throw ApiPromiseConnection.handleError(e);
}),
),
);
Expand Down