Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
runat-1128988: Pass Errors through protocol.js correctly
Browse files Browse the repository at this point in the history
In Output.toJson we were assuming that `JSON.stringify(new Error('x'))`
would give us something useful. It actually just gives `{}`, so we
manually extract the properties of a standard Error.

We were using `error:true` to denote an error, but this was conflicting
with a special property in protocol.js, so we're using isError instead.
(Commands that fail are considered non-exceptional. Exceptions in GCLI
are for when GCLI itself fails)

The converters now need to cope with errors that are objects that look
like Errors (but not actually errors).

In the process of debugging this I refactored the code that kicks off
a remote execution in system.js.

Signed-off-by: Joe Walker <[email protected]>
  • Loading branch information
joewalker committed Mar 12, 2015
1 parent d374a91 commit b04f95a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
16 changes: 14 additions & 2 deletions lib/gcli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2164,11 +2164,23 @@ Output.prototype.convert = function(type, conversionContext) {
};

Output.prototype.toJson = function() {
// Exceptions don't stringify, so we try a bit harder
var data = this.data;
if (this.error && JSON.stringify(this.data) === '{}') {
data = {
columnNumber: data.columnNumber,
fileName: data.fileName,
lineNumber: data.lineNumber,
message: data.message,
stack: data.stack
};
}

return {
typed: this.typed,
type: this.type,
data: this.data,
error: this.error
data: data,
isError: this.error
};
};

Expand Down
11 changes: 10 additions & 1 deletion lib/gcli/converters/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var errorDomConverter = {
exec: function(ex, conversionContext) {
var node = util.createElement(conversionContext.document, 'p');
node.className = 'gcli-error';
node.textContent = ex;
node.textContent = errorStringConverter.exec(ex, conversionContext);
return node;
}
};
Expand All @@ -112,6 +112,15 @@ var errorStringConverter = {
from: 'error',
to: 'string',
exec: function(ex, conversionContext) {
if (typeof ex === 'string') {
return ex;
}
if (ex instanceof Error) {
return '' + ex;
}
if (typeof ex.message === 'string') {

This comment has been minimized.

Copy link
@bgrins

bgrins Mar 13, 2015

Do you need to null check ex here? Not sure if that will ever be passed in, but it will now throw if that happens whereas before it wouldn't

This comment has been minimized.

Copy link
@joewalker

joewalker Mar 20, 2015

Author Owner

I think we're ok.
I remember recently adding something to the test harness to cope with null values, because GCLI was ok with something that helpers.js wasn't.

return ex.message;
}
return '' + ex;
}
};
Expand Down
8 changes: 1 addition & 7 deletions lib/gcli/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,9 @@ function addLocalFunctions(specs, front) {
if (!commandSpec.isParent) {
commandSpec.exec = function(args, context) {
var typed = (context.prefix ? context.prefix + ' ' : '') + context.typed;

return front.execute(typed).then(function(reply) {
var typedData = context.typedData(reply.type, reply.data);
if (!reply.error) {
return typedData;
}
else {
throw typedData;
}
return reply.isError ? Promise.reject(typedData) : typedData;

This comment has been minimized.

Copy link
@bgrins

bgrins Mar 13, 2015

Just to clarify - this is equivalent behavior, right? Throwing foo inside of the callback will have the same outcome as returning Promise.reject(foo)?

This comment has been minimized.

Copy link
@joewalker

joewalker Mar 16, 2015

Author Owner

Yes. Just more compact, and equally readable, to my eyes anyway!

This comment has been minimized.

Copy link
@bgrins

bgrins Mar 16, 2015

Yeah, I think it's easier to follow the logic

});
};
}
Expand Down

0 comments on commit b04f95a

Please sign in to comment.