Skip to content

Commit

Permalink
Use transaction id from error to print out explorer link for errors (#…
Browse files Browse the repository at this point in the history
…463)

* Use transaction id from error to print out explorer link for errors

* Update near-api-js version to latest and tweak the error format

* Update utils/inspect-response.js

Co-authored-by: Mike Purvis <mikedotexe@gmail.com>

Co-authored-by: Mike Purvis <mikedotexe@gmail.com>
  • Loading branch information
janedegtiareva and mikedotexe committed Jul 28, 2020
1 parent c101014 commit 46ba4d3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 4 additions & 2 deletions utils/exit-on-error.js
@@ -1,4 +1,5 @@
const eventtracking = require('./eventtracking');
const inspectResponse = require('./inspect-response');

// This is a workaround to get Mixpanel to log a crash
process.on('exit', () => {
Expand All @@ -21,7 +22,8 @@ module.exports = (promiseFn) => async (...args) => {
const command = args[0]['_'];
process.env.NEAR_CLI_ERROR_LAST_COMMAND = command;
process.env.NEAR_CLI_NETWORK_ID = require('../get-config')()['networkId'];
const optionsAsStr = JSON.stringify(args[0]);
const options = args[0];
const optionsAsStr = JSON.stringify(options);
const eventId = `event_id_shell_${command}_start`;
require('child_process').fork(__dirname + '/log-event.js', ['node'], {
silent: true,
Expand All @@ -38,7 +40,7 @@ module.exports = (promiseFn) => async (...args) => {
} catch (e) {
process.env.NEAR_CLI_LAST_ERROR = e.message;
process.env.NEAR_CLI_OPTIONS = optionsAsStr;
console.log('Error: ', e);
inspectResponse.prettyPrintError(e, options);
process.exit(1);
}
};
32 changes: 31 additions & 1 deletion utils/inspect-response.js
Expand Up @@ -10,9 +10,38 @@ const prettyPrintResponse = (response, options) => {
explorer.printTransactionUrl(txnId, options);
};

const prettyPrintError = (error, options) => {
console.log('An error occured');
console.log(formatResponse(error));
const txnId = getTxnIdFromError(error);
if (txnId) {
console.log(`We attempted to send transaction ${txnId} to NEAR, but something went wrong.`);
explorer.printTransactionUrl(txnId, options);
console.log('Note: if the transaction was invalid (e.g. not enough balance), it will show as "Not started" or "Finalizing"');
}
};

const formatResponse = (response) => {
return util.inspect(response, { showHidden: true, depth: null, colors: true, maxArrayLength: null });
};

const getTxnIdFromError = (error) => {
// Currently supported error format:
// {
// [stack]: 'Error: Sender jane.betanet does not have enough balance 45000000521675913419670000 for operation costing 1000000000002265303009375000\n' +
// ...
// [message]: 'Sender jane.betanet does not have enough balance 45000000521675913419670000 for operation costing 1000000000002265303009375000',
// type: 'NotEnoughBalance',
// context: ErrorContext {
// transactionHash: 'FyavUCyvZ5G1JLTdnXSZd3VoaFEaGRXnmDFwhmNeaVC6'
// },
// balance: '45000000521675913419670000',
// cost: '1000000000002265303009375000',
// signer_id: 'jane.betanet'
// }

if (!error || !error.context) return null;
return error.context.transactionHash;
};

const getTxnId = (response) => {
Expand All @@ -31,6 +60,7 @@ const getTxnId = (response) => {

module.exports = {
prettyPrintResponse,
prettyPrintError,
formatResponse,
getTxnId,
};
};

0 comments on commit 46ba4d3

Please sign in to comment.