Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

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

Merged
merged 4 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions utils/exit-on-error.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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,
};
};