Skip to content

Commit

Permalink
refactor(@embark/core/console): silence errors when autocomplete is a…
Browse files Browse the repository at this point in the history
…ttempted on bad reference

Previously, in the embark cli dashboard/console, if you were to type `Foo.` and
hit tab for autocomplete then (assuming you hadn't defined `Foo`) there would
be unhandled errors and the console could even become unusable.

Refactor `packages/core/console` and related code so that nothing happens when
you attempt to autocomplete a bad reference (the same behavior as Node's own
REPL).
  • Loading branch information
michaelsbradleyjr committed Nov 5, 2019
1 parent 6b405d1 commit c320dcf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
10 changes: 7 additions & 3 deletions packages/core/code-runner/src/index.ts
Expand Up @@ -52,7 +52,7 @@ class CodeRunner {
this.vm.registerVar(varName, code, cb);
}

private evalCode(code: string, cb: Callback<any>, tolerateError = false) {
private evalCode(code: string, cb: Callback<any>, tolerateError = false, logCode = true, logError = true) {
cb = cb || (() => { });

if (!code) {
Expand All @@ -61,8 +61,12 @@ class CodeRunner {

this.vm.doEval(code, tolerateError, (err, result) => {
if (err) {
this.logger.error(__("Error running code: %s", code));
this.logger.error(err.toString());
if (logCode) {
this.logger.error(__("Error running code: %s", code));
}
if (logError) {
this.logger.error(err.toString());
}
return cb(err);
}

Expand Down
35 changes: 24 additions & 11 deletions packages/core/console/src/lib/index.ts
Expand Up @@ -47,13 +47,18 @@ class Console {

if (this.ipc.isServer()) {
this.ipc.on("console:executeCmd", (cmd: string, cb: any) => {
this.executeCmd(cmd, (err: string, result: any) => {
let error = null;
this.executeCmd(cmd, (err: any, result: any) => {
if (err) {
// reformat for IPC reply
error = { name: "Console error", message: err, stack: err };
err = { name: "Console error", message: err, stack: err.stack };
return cb(err);
}
cb(error, util.inspect(result));
cb(null, util.inspect(result));
});
});
this.ipc.on("console:executePartial", (cmd: string, cb: any) => {
this.executePartial(cmd, (_: any, result: any) => {
cb(null, util.inspect(result));
});
});
this.ipc.on("console:history:save", true, (cmd: string) => {
Expand All @@ -70,6 +75,7 @@ class Console {
if (cb) { cb(); }
});
this.events.setCommandHandler("console:executeCmd", this.executeCmd.bind(this));
this.events.setCommandHandler("console:executePartial", this.executePartial.bind(this));
this.events.setCommandHandler("console:history", (cb: any) => this.getHistory(this.cmdHistorySize(), cb));
this.registerConsoleCommands();

Expand Down Expand Up @@ -149,7 +155,19 @@ class Console {
return false;
}

private executeCmd(cmd: string, callback: any) {
private executePartial(cmd: string, callback: any) {
// if this is the embark console process, send the command to the process
// running all the needed services (ie the process running `embark run`)
if (this.isEmbarkConsole) {
return this.ipc.request("console:executePartial", cmd, callback);
}

this.executeCmd(cmd, (_: any, result: any) => {
callback(null, result);
});
}

private executeCmd(cmd: string, callback?: any, logEvalCode = false, logEvalError = false) {
// if this is the embark console process, send the command to the process
// running all the needed services (ie the process running `embark run`)
if (this.isEmbarkConsole) {
Expand Down Expand Up @@ -197,12 +215,7 @@ class Console {
return callback(null, output);
}

this.events.request("runcode:eval", cmd, (err: Error, result: any) => {
if (err) {
return callback(err.message);
}
callback(null, result);
}, true);
this.events.request("runcode:eval", cmd, callback, true, logEvalCode, logEvalError);
}

private registerConsoleCommands() {
Expand Down
8 changes: 4 additions & 4 deletions packages/embark/src/cmd/dashboard/repl.js
Expand Up @@ -27,7 +27,7 @@ class REPL {
this.events.request('console:executeCmd', cmd.trim(), function (err, message) {
if (err) {
// Do not return as the first param (error), because the dashboard doesn't print errors
return callback(null, err.red);
return callback(null, err.message ? err.message.red : err.red);
}
callback(null, message === undefined ? '' : message); // This way, we don't print undefined
});
Expand Down Expand Up @@ -58,9 +58,9 @@ class REPL {
hint = partial;
}

this.events.request('console:executeCmd', context, (err, result) => {
if (err !== null) {
cb(err, [[], partial]);
this.events.request('console:executePartial', context, (err, result) => {
if (err || !result) {
return cb(null, [[], partial]);
}

let props = Object
Expand Down

0 comments on commit c320dcf

Please sign in to comment.