@rockset/cli
Version:
Official Rockset CLI
105 lines (102 loc) • 4.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RockCommand = void 0;
const core_1 = require("@oclif/core");
const helper_1 = require("@rockset/core/dist/helper");
const _ = require("lodash");
const exception_1 = require("@rockset/core/dist/exception/exception");
const errors_1 = require("@oclif/errors");
const core_2 = require("@rockset/core");
const abort_controller_1 = require("abort-controller");
const chalk = require("chalk");
// Globally handle all unhandled promise rejections
process.on('unhandledRejection', function (reason, p) {
// If we aren't in debug mode, swallow unhandled promise rejections
if (process.env.ROCKSET_CLI_DEBUG_MODE) {
// Log unhandled promise errors if we are in debug mode
console.error('Unhandled', reason, p);
console.error('This is likely a bug in the Rockset CLI. Please contact Rockset Support with the error message to resolve this issue.');
}
});
class RockCommand extends core_1.Command {
async catch(err) {
if (err instanceof errors_1.CLIError) {
// This is necessary because oclif seems to throw errors sometimes to exit logic early...
if ((err === null || err === void 0 ? void 0 : err.code) !== 'EEXIT') {
// Log the error by itself. Skip logging the error object because it is totally unhelpful
this.error(err);
}
return;
}
this.logTelemetrics({ error: err });
const finalErr = `
${_.truncate(helper_1.prettyPrint(err), { length: 500 })}
${err}
`;
this.error(finalErr);
}
info(param) {
console.error(chalk.gray `[INFO]: ${param}`);
}
async init() {
this.logTelemetrics();
}
logTelemetrics({ error, } = {}) {
try {
// Don't want to log the actual error object
const errorId = error && error instanceof exception_1.RockClientException ? error.id : null;
const { name: commandClassName } = this.constructor;
const { version, platform, arch, shell } = this.config;
const commandName = this.id;
const timestamp = Date.now();
const readable = new Date();
const timestampReadable = readable.toUTCString();
const type = errorId ? 'ERROR' : 'INFO';
const allTelemetrics = {
commandClassName,
version,
platform,
arch,
shell,
commandName,
timestamp,
timestampReadable,
type,
errorId,
};
// Don't block for this
if (!process.env.ROCKSET_CLI_TELEMETRY_OPTOUT) {
const controller = new abort_controller_1.default();
core_2.main
.createClient()
.then(async (client) => {
const { api_server } = await core_2.auth.getAuthProfile();
const body = JSON.stringify({
event: allTelemetrics,
});
const endpoint = `${api_server}/v1/telemetry`;
const options = {
method: 'POST',
body,
headers: {
'Content-Type': 'application/json',
},
signal: controller.signal,
};
const prom = client._fetch(endpoint, options);
// cancel the request in 50ms
// Note that this just rejects the promise, doesn't cancel a request that has been sent
// Doing this prevents the process from staying alive if the request is taking too long
await helper_1.wait(50);
controller.abort();
return prom;
})
.catch(() => { });
}
}
catch (_) {
// Something went wrong, fail silently
}
}
}
exports.RockCommand = RockCommand;