feat(web3): add send transaction error with logs #18277 (#18606)

* feat(web3): add send transaction error with logs #18277

* fix: logs type

* refactor: prettier
This commit is contained in:
Alexey Elizarov 2021-07-13 17:14:20 +03:00 committed by GitHub
parent 90f8cf0920
commit 33066d254e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

View File

@ -26,6 +26,7 @@ import {IWSRequestParams} from 'rpc-websockets/dist/lib/client';
import {AgentManager} from './agent-manager'; import {AgentManager} from './agent-manager';
import {EpochSchedule} from './epoch-schedule'; import {EpochSchedule} from './epoch-schedule';
import {SendTransactionError} from './errors';
import {NonceAccount} from './nonce-account'; import {NonceAccount} from './nonce-account';
import {PublicKey} from './publickey'; import {PublicKey} from './publickey';
import {Signer} from './keypair'; import {Signer} from './keypair';
@ -3444,7 +3445,19 @@ export class Connection {
const unsafeRes = await this._rpcRequest('simulateTransaction', args); const unsafeRes = await this._rpcRequest('simulateTransaction', args);
const res = create(unsafeRes, SimulatedTransactionResponseStruct); const res = create(unsafeRes, SimulatedTransactionResponseStruct);
if ('error' in res) { 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; return res.result;
} }
@ -3528,15 +3541,19 @@ export class Connection {
const unsafeRes = await this._rpcRequest('sendTransaction', args); const unsafeRes = await this._rpcRequest('sendTransaction', args);
const res = create(unsafeRes, SendTransactionRpcResult); const res = create(unsafeRes, SendTransactionRpcResult);
if ('error' in res) { if ('error' in res) {
let logs;
if ('data' in res.error) { if ('data' in res.error) {
const logs = res.error.data.logs; logs = res.error.data.logs;
if (logs && Array.isArray(logs)) { if (logs && Array.isArray(logs)) {
const traceIndent = '\n '; const traceIndent = '\n ';
const logTrace = traceIndent + logs.join(traceIndent); const logTrace = traceIndent + logs.join(traceIndent);
console.error(res.error.message, logTrace); 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; return res.result;
} }

9
web3.js/src/errors.ts Normal file
View File

@ -0,0 +1,9 @@
export class SendTransactionError extends Error {
logs: string[] | undefined;
constructor(message: string, logs?: string[]) {
super(message);
this.logs = logs;
}
}