[go: nahoru, domu]

Skip to content

Commit

Permalink
Add unknown error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
james-hummingbot committed Oct 15, 2021
1 parent 28fde54 commit 352ff47
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ coverage.xml
/gateway/dist/

# misc
/gateway/coverage/
/gateway/logs/
/gateway/package-lock.json
*.srl
Expand Down
3 changes: 2 additions & 1 deletion gateway/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
certs
*.md
*.yml
*.yml
coverage
66 changes: 35 additions & 31 deletions gateway/src/chains/ethereum/ethereum.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,44 +218,48 @@ export async function poll(
): Promise<EthereumPollResponse> {
validateEthereumPollRequest(req);

const initTime = Date.now();
const currentBlock = await ethereum.getCurrentBlockNumber();
const txData = await ethereum.getTransaction(req.txHash);
let txBlock, txReceipt, txStatus;
if (!txData) {
// tx not found, didn't reach the mempool or it never existed
txBlock = -1;
txReceipt = null;
txStatus = -1;
} else {
txReceipt = await ethereum.getTransactionReceipt(req.txHash);
if (txReceipt === null) {
// tx is in the mempool
try {
const initTime = Date.now();
const currentBlock = await ethereum.getCurrentBlockNumber();
const txData = await ethereum.getTransaction(req.txHash);
let txBlock, txReceipt, txStatus;
if (!txData) {
// tx not found, didn't reach the mempool or it never existed
txBlock = -1;
txReceipt = null;
txStatus = -1;
} else {
// tx has been processed
txBlock = txReceipt.blockNumber;
txStatus = typeof txReceipt.status === 'number' ? txReceipt.status : -1;
if (txStatus === 0) {
const gasUsed = BigNumber.from(txReceipt.gasUsed).toNumber();
const gasLimit = BigNumber.from(txData.gasLimit).toNumber();
if (gasUsed / gasLimit > 0.9)
throw new GatewayError(503, 1003, 'Transaction out of gas.');
txReceipt = await ethereum.getTransactionReceipt(req.txHash);
if (txReceipt === null) {
// tx is in the mempool
txBlock = -1;
txReceipt = null;
txStatus = -1;
} else {
// tx has been processed
txBlock = txReceipt.blockNumber;
txStatus = typeof txReceipt.status === 'number' ? txReceipt.status : -1;
if (txStatus === 0) {
const gasUsed = BigNumber.from(txReceipt.gasUsed).toNumber();
const gasLimit = BigNumber.from(txData.gasLimit).toNumber();
if (gasUsed / gasLimit > 0.9)
throw new GatewayError(503, 1003, 'Transaction out of gas.');
}
}
}
return {
network: ConfigManager.config.ETHEREUM_CHAIN,
currentBlock,
timestamp: initTime,
txHash: req.txHash,
txBlock,
txStatus,
txData,
txReceipt: toEthereumTransactionReceipt(txReceipt),
};
} catch (_e) {
throw new GatewayError(503, 1099, 'Unknown error.');
}
return {
network: ConfigManager.config.ETHEREUM_CHAIN,
currentBlock,
timestamp: initTime,
txHash: req.txHash,
txBlock,
txStatus,
txData,
txReceipt: toEthereumTransactionReceipt(txReceipt),
};
}

export async function cancel(
Expand Down
16 changes: 8 additions & 8 deletions gateway/src/services/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { Request, RequestHandler, Response, NextFunction } from 'express';
// custom error for http exceptions
export class HttpException extends Error {
status: number;
message: string;
constructor(status: number, message: string) {
super(message);
errorMessage: string;
constructor(status: number, errorMessage: string) {
super(errorMessage);
this.status = status;
this.message = message;
this.errorMessage = errorMessage;
}
}

export class GatewayError extends Error {
message: string;
errorMessage: string;
errorCode: number;
httpErrorCode: number;
constructor(httpErrorCode: number, errorCode: number, message: string) {
super(message);
constructor(httpErrorCode: number, errorCode: number, errorMessage: string) {
super(errorMessage);
this.httpErrorCode = httpErrorCode;
this.errorCode = errorCode;
this.message = message;
this.errorMessage = errorMessage;
}
}

Expand Down

0 comments on commit 352ff47

Please sign in to comment.