Skip to content

Commit

Permalink
Remove extra logging of ClientConnectorError (#279)
Browse files Browse the repository at this point in the history
* [skip ci]
  • Loading branch information
Nathan-SL committed Dec 21, 2022
1 parent 8cb85b3 commit 2a9940b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
65 changes: 39 additions & 26 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import * as fs from "fs";
import * as starknet from "./starknet-types";
import { StarknetPluginError } from "./starknet-plugin-error";
import {
CHECK_STATUS_TIMEOUT,
CHECK_STATUS_RECOVER_TIMEOUT,
QUERY_VERSION,
TRANSACTION_VERSION,
HEXADECIMAL_REGEX
HEXADECIMAL_REGEX,
CHECK_STATUS_TIMEOUT
} from "./constants";
import { adaptLog, copyWithBigint, warn } from "./utils";
import { adaptLog, copyWithBigint, sleep, warn } from "./utils";
import { adaptInputUtil, adaptOutputUtil } from "./adapt";
import { HardhatRuntimeEnvironment, Wallet } from "hardhat/types";
import { hash } from "starknet";
Expand Down Expand Up @@ -161,32 +161,45 @@ export async function iterativelyCheckStatus(
txHash: string,
starknetWrapper: StarknetWrapper,
resolve: (status: string) => void,
reject: (reason: Error) => void
reject: (reason: Error) => void,
retryCount = 10
) {
const statusObject = await checkStatus(txHash, starknetWrapper).catch((reason) => {
warn(reason);
return undefined;
});
// eslint-disable-next-line no-constant-condition
while (true) {
let count = retryCount;
let statusObject;
let error;
while (count > 0) {
// This promise is rejected usually if the network is unavailable
statusObject = await checkStatus(txHash, starknetWrapper).catch((reason) => {
error = reason;
return undefined;
});
// Check count at 1 to avoid unnecessary waiting(sleep) in the last iteration
if (statusObject || count === 1) {
break;
}

if (!statusObject) {
warn("Retrying transaction status check...");
// eslint-disable-next-line prefer-rest-params
setTimeout(iterativelyCheckStatus, CHECK_STATUS_RECOVER_TIMEOUT, ...arguments);
} else if (isTxAccepted(statusObject)) {
resolve(statusObject.tx_status);
} else if (isTxRejected(statusObject)) {
reject(
new Error(
"Transaction rejected. Error message:\n\n" +
adaptLog(statusObject.tx_failure_reason.error_message)
)
);
} else {
// Make a recursive call, but with a delay.
// Local var `arguments` holds what was passed in the current call
await sleep(CHECK_STATUS_RECOVER_TIMEOUT);
warn("Retrying transaction status check...");
count--;
}

if (!statusObject) {
warn("Checking transaction status failed.");
return reject(error);
} else if (isTxAccepted(statusObject)) {
return resolve(statusObject.tx_status);
} else if (isTxRejected(statusObject)) {
return reject(
new Error(
"Transaction rejected. Error message:\n\n" +
adaptLog(statusObject.tx_failure_reason.error_message)
)
);
}

// eslint-disable-next-line prefer-rest-params
setTimeout(iterativelyCheckStatus, CHECK_STATUS_TIMEOUT, ...arguments);
await sleep(CHECK_STATUS_TIMEOUT);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ export function getImageTagByArch(tag: string): string {
return tag;
}

export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* Log a yellow message to STDERR.
* @param message
Expand Down

0 comments on commit 2a9940b

Please sign in to comment.