diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 865dd4d1e2..ee21927198 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -26,6 +26,7 @@ import {IWSRequestParams} from 'rpc-websockets/dist/lib/client'; import {AgentManager} from './agent-manager'; import {EpochSchedule} from './epoch-schedule'; +import {SendTransactionError} from './errors'; import {NonceAccount} from './nonce-account'; import {PublicKey} from './publickey'; import {Signer} from './keypair'; @@ -3444,7 +3445,19 @@ export class Connection { const unsafeRes = await this._rpcRequest('simulateTransaction', args); const res = create(unsafeRes, SimulatedTransactionResponseStruct); if ('error' in res) { - throw new Error('failed to simulate transaction: ' + res.error.message); + let logs; + if ('data' in res.error) { + logs = res.error.data.logs; + if (logs && Array.isArray(logs)) { + const traceIndent = '\n '; + const logTrace = traceIndent + logs.join(traceIndent); + console.error(res.error.message, logTrace); + } + } + throw new SendTransactionError( + 'failed to simulate transaction: ' + res.error.message, + logs, + ); } return res.result; } @@ -3528,15 +3541,19 @@ export class Connection { const unsafeRes = await this._rpcRequest('sendTransaction', args); const res = create(unsafeRes, SendTransactionRpcResult); if ('error' in res) { + let logs; if ('data' in res.error) { - const logs = res.error.data.logs; + logs = res.error.data.logs; if (logs && Array.isArray(logs)) { const traceIndent = '\n '; const logTrace = traceIndent + logs.join(traceIndent); console.error(res.error.message, logTrace); } } - throw new Error('failed to send transaction: ' + res.error.message); + throw new SendTransactionError( + 'failed to send transaction: ' + res.error.message, + logs, + ); } return res.result; } diff --git a/web3.js/src/errors.ts b/web3.js/src/errors.ts new file mode 100644 index 0000000000..c1155137b3 --- /dev/null +++ b/web3.js/src/errors.ts @@ -0,0 +1,9 @@ +export class SendTransactionError extends Error { + logs: string[] | undefined; + + constructor(message: string, logs?: string[]) { + super(message); + + this.logs = logs; + } +}