/
exit-on-error.js
46 lines (44 loc) · 1.6 KB
/
exit-on-error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const eventtracking = require('./eventtracking');
const inspectResponse = require('./inspect-response');
// This is a workaround to get Mixpanel to log a crash
process.on('exit', () => {
const crashEventProperties = {
near_cli_command: process.env.NEAR_CLI_ERROR_LAST_COMMAND,
error_message: process.env.NEAR_CLI_LAST_ERROR
};
require('child_process').fork(__dirname + '/log-event.js', ['node'], {
silent: true,
detached: true,
env: {
NEAR_CLI_EVENT_ID: eventtracking.EVENT_ID_ERROR,
NEAR_CLI_EVENT_DATA: JSON.stringify(crashEventProperties),
NEAR_CLI_OPTIONS: process.env.NEAR_CLI_OPTIONS
}
});
});
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 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,
detached: true,
env: {
NEAR_CLI_EVENT_ID: eventId,
NEAR_CLI_EVENT_DATA: JSON.stringify({}),
NEAR_CLI_OPTIONS: optionsAsStr
}
});
const promise = promiseFn.apply(null, args);
try {
await promise;
} catch (e) {
process.env.NEAR_CLI_LAST_ERROR = e.message;
process.env.NEAR_CLI_OPTIONS = optionsAsStr;
inspectResponse.prettyPrintError(e, options);
process.exit(1);
}
};