gecko-dev/remote/test/unit/test_Error.js
Andreas Tolfsen 3c8ae0e869 bug 1546722: remote: print better errors in browser chrome tests; r=yulia
We can reconstruct the CDP error and print a better error message
using RemoteAgentError.  This is better than the current output,
which in a lot of cases will be "CDP Exception: [object Object]".

A possible future improvement to this involves extending the CDP
protocol to send the individual components of the error individually,
so that we don't have to parse the string.

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

--HG--
extra : moz-landing-system : lando
2019-04-25 15:16:46 +00:00

104 lines
2.4 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
/* eslint-disable no-tabs */
const {
RemoteAgentError,
UnknownMethodError,
UnsupportedError,
} = ChromeUtils.import("chrome://remote/content/Error.jsm");
add_test(function test_RemoteAgentError_ctor() {
const e1 = new RemoteAgentError();
equal(e1.name, "RemoteAgentError");
equal(e1.message, "");
equal(e1.cause, e1.message);
const e2 = new RemoteAgentError("message");
equal(e2.message, "message");
equal(e2.cause, e2.message);
const e3 = new RemoteAgentError("message", "cause");
equal(e3.message, "message");
equal(e3.cause, "cause");
run_next_test();
});
add_test(function test_RemoteAgentError_notify() {
// nothing much we can test, except test that it doesn't throw
new RemoteAgentError().notify();
run_next_test();
});
add_test(function test_RemoteAgentError_toString() {
const e = new RemoteAgentError("message");
equal(e.toString(), RemoteAgentError.format(e));
equal(e.toString({stack: true}), RemoteAgentError.format(e, {stack: true}));
run_next_test();
});
add_test(function test_RemoteAgentError_format() {
const {format} = RemoteAgentError;
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, {stack: true}),
`DogError: woof:
one
two
three`);
const cat = {
name: "CatError",
message: "meow",
stack: "four\nfive\nsix",
cause: dog,
};
equal(format(cat), "CatError: meow");
equal(format(cat, {stack: true}),
`CatError: meow:
four
five
six
caused by: DogError: woof:
one
two
three`);
run_next_test();
});
add_test(function test_RemoteAgentError_fromJSON() {
const cdpErr = {message: `TypeError: foo:
bar
baz`};
const err = RemoteAgentError.fromJSON(cdpErr);
equal(err.message, "TypeError: foo");
equal(err.stack, "bar\nbaz");
equal(err.cause, null);
run_next_test();
});
add_test(function test_UnsupportedError() {
ok(new UnsupportedError() instanceof RemoteAgentError);
run_next_test();
});
add_test(function test_UnknownMethodError() {
ok(new UnknownMethodError() instanceof RemoteAgentError);
run_next_test();
});