bug 1540684: remote: fix stray colons in error formatting; r=ochameau

Error messages contained extraneous colons that were inconsistent
with how JavaScript errors are usually formatted.

Examples of ill-formed formatting:

	FooError:

	FooError: bar:

The trailing colons should not be present in either of these cases.
Colons should only be printed when something follows.  For example:

	FooError

	FooError: bar

	FooError: bar:
		test.js:42

Differential Revision: https://phabricator.services.mozilla.com/D25608

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Andreas Tolfsen 2019-04-04 09:55:14 +00:00
parent 924a19a629
commit 9936854ecf
3 changed files with 17 additions and 9 deletions

View file

@ -3,5 +3,6 @@
module.exports = {
"rules": {
"max-len": "off",
"no-tabs": "off",
}
};

View file

@ -71,18 +71,25 @@ class UnsupportedError extends RemoteAgentError {}
class UnknownMethodError extends RemoteAgentError {}
function formatError(error, {stack = false} = {}) {
const ls = [];
const els = [];
ls.push(`${error.name}: ${error.message ? `${error.message}:` : ""}`);
els.push(error.name);
if (error.message) {
els.push(": ");
els.push(error.message);
}
if (stack && error.stack) {
els.push(":\n");
const stack = error.stack.trim().split("\n");
ls.push(stack.map(line => `\t${line}`).join("\n"));
els.push(stack.map(line => `\t${line}`).join("\n"));
if (error.cause) {
ls.push("caused by: " + formatError(error.cause, {stack}));
els.push("\n");
els.push("caused by: " + formatError(error.cause, {stack}));
}
}
return ls.join("\n");
return els.join("");
}

View file

@ -46,15 +46,15 @@ add_test(function test_RemoteAgentError_toString() {
add_test(function test_RemoteAgentError_format() {
const {format} = RemoteAgentError;
equal(format({name: "HippoError"}), "HippoError: ");
equal(format({name: "HorseError", message: "neigh"}), "HorseError: neigh:");
equal(format({name: "HippoError"}), "HippoError");
equal(format({name: "HorseError", message: "neigh"}), "HorseError: neigh");
const dog = {
name: "DogError",
message: "woof",
stack: " one\ntwo\nthree ",
};
equal(format(dog), "DogError: woof:");
equal(format(dog), "DogError: woof");
equal(format(dog, {stack: true}),
`DogError: woof:
one
@ -67,7 +67,7 @@ add_test(function test_RemoteAgentError_format() {
stack: "four\nfive\nsix",
cause: dog,
};
equal(format(cat), "CatError: meow:");
equal(format(cat), "CatError: meow");
equal(format(cat, {stack: true}),
`CatError: meow:
four