diff --git a/utils/exit-on-error.js b/utils/exit-on-error.js index b6ec89b4..53510d49 100644 --- a/utils/exit-on-error.js +++ b/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', () => { @@ -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, @@ -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); } }; \ No newline at end of file diff --git a/utils/inspect-response.js b/utils/inspect-response.js index edf96c14..4c95db92 100644 --- a/utils/inspect-response.js +++ b/utils/inspect-response.js @@ -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) => { @@ -31,6 +60,7 @@ const getTxnId = (response) => { module.exports = { prettyPrintResponse, + prettyPrintError, formatResponse, getTxnId, -}; \ No newline at end of file +};