Skip to content

Commit 8279120

Browse files
committed
Better error messaging when provider backends give bogus responses (#1243).
1 parent 243beff commit 8279120

File tree

1 file changed

+68
-8
lines changed

1 file changed

+68
-8
lines changed

packages/providers/src.ts/base-provider.ts

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ let defaultFormatter: Formatter = null;
422422

423423
let nextPollId = 1;
424424

425-
426425
export class BaseProvider extends Provider implements EnsProvider {
427426
_networkPromise: Promise<Network>;
428427
_network: Network;
@@ -970,7 +969,16 @@ export class BaseProvider extends Provider implements EnsProvider {
970969

971970
async getGasPrice(): Promise<BigNumber> {
972971
await this.getNetwork();
973-
return BigNumber.from(await this.perform("getGasPrice", { }));
972+
973+
const result = await this.perform("getGasPrice", { });
974+
try {
975+
return BigNumber.from(result);
976+
} catch (error) {
977+
return logger.throwError("bad result from backend", Logger.errors.SERVER_ERROR, {
978+
method: "getGasPrice",
979+
result, error
980+
});
981+
}
974982
}
975983

976984
async getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<BigNumber> {
@@ -979,7 +987,16 @@ export class BaseProvider extends Provider implements EnsProvider {
979987
address: this._getAddress(addressOrName),
980988
blockTag: this._getBlockTag(blockTag)
981989
});
982-
return BigNumber.from(await this.perform("getBalance", params));
990+
991+
const result = await this.perform("getBalance", params);
992+
try {
993+
return BigNumber.from(result);
994+
} catch (error) {
995+
return logger.throwError("bad result from backend", Logger.errors.SERVER_ERROR, {
996+
method: "getBalance",
997+
params, result, error
998+
});
999+
}
9831000
}
9841001

9851002
async getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<number> {
@@ -988,7 +1005,16 @@ export class BaseProvider extends Provider implements EnsProvider {
9881005
address: this._getAddress(addressOrName),
9891006
blockTag: this._getBlockTag(blockTag)
9901007
});
991-
return BigNumber.from(await this.perform("getTransactionCount", params)).toNumber();
1008+
1009+
const result = await this.perform("getTransactionCount", params);
1010+
try {
1011+
return BigNumber.from(result).toNumber();
1012+
} catch (error) {
1013+
return logger.throwError("bad result from backend", Logger.errors.SERVER_ERROR, {
1014+
method: "getTransactionCount",
1015+
params, result, error
1016+
});
1017+
}
9921018
}
9931019

9941020
async getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
@@ -997,7 +1023,16 @@ export class BaseProvider extends Provider implements EnsProvider {
9971023
address: this._getAddress(addressOrName),
9981024
blockTag: this._getBlockTag(blockTag)
9991025
});
1000-
return hexlify(await this.perform("getCode", params));
1026+
1027+
const result = await this.perform("getCode", params);
1028+
try {
1029+
return hexlify(result);
1030+
} catch (error) {
1031+
return logger.throwError("bad result from backend", Logger.errors.SERVER_ERROR, {
1032+
method: "getCode",
1033+
params, result, error
1034+
});
1035+
}
10011036
}
10021037

10031038
async getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
@@ -1007,7 +1042,15 @@ export class BaseProvider extends Provider implements EnsProvider {
10071042
blockTag: this._getBlockTag(blockTag),
10081043
position: Promise.resolve(position).then((p) => hexValue(p))
10091044
});
1010-
return hexlify(await this.perform("getStorageAt", params));
1045+
const result = await this.perform("getStorageAt", params);
1046+
try {
1047+
return hexlify(result);
1048+
} catch (error) {
1049+
return logger.throwError("bad result from backend", Logger.errors.SERVER_ERROR, {
1050+
method: "getStorageAt",
1051+
params, result, error
1052+
});
1053+
}
10111054
}
10121055

10131056
// This should be called by any subclass wrapping a TransactionResponse
@@ -1115,15 +1158,32 @@ export class BaseProvider extends Provider implements EnsProvider {
11151158
transaction: this._getTransactionRequest(transaction),
11161159
blockTag: this._getBlockTag(blockTag)
11171160
});
1118-
return hexlify(await this.perform("call", params));
1161+
1162+
const result = await this.perform("call", params);
1163+
try {
1164+
return hexlify(result);
1165+
} catch (error) {
1166+
return logger.throwError("bad result from backend", Logger.errors.SERVER_ERROR, {
1167+
method: "call",
1168+
params, result, error
1169+
});
1170+
}
11191171
}
11201172

11211173
async estimateGas(transaction: Deferrable<TransactionRequest>): Promise<BigNumber> {
11221174
await this.getNetwork();
11231175
const params = await resolveProperties({
11241176
transaction: this._getTransactionRequest(transaction)
11251177
});
1126-
return BigNumber.from(await this.perform("estimateGas", params));
1178+
const result = await this.perform("estimateGas", params);
1179+
try {
1180+
return BigNumber.from(result);
1181+
} catch (error) {
1182+
return logger.throwError("bad result from backend", Logger.errors.SERVER_ERROR, {
1183+
method: "estimateGas",
1184+
params, result, error
1185+
});
1186+
}
11271187
}
11281188

11291189
async _getAddress(addressOrName: string | Promise<string>): Promise<string> {

0 commit comments

Comments
 (0)