Bug 1410652 - Use invalid argument error for web element deserialisation. r=whimboo

For user input we will want to return the appropriate invalid
argument error.  For internal input using TypeError is fine.

MozReview-Commit-ID: AlOnZuhaczN

--HG--
extra : rebase_source : f16aa13b6fc53da6261594dab0c9df34d6c3df6e
This commit is contained in:
Andreas Tolfsen 2017-10-24 17:42:32 +01:00
Родитель f9a44482f3
Коммит 089e754126
2 изменённых файлов: 21 добавлений и 23 удалений

Просмотреть файл

@ -10,6 +10,7 @@ const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("chrome://marionette/content/assert.js");
Cu.import("chrome://marionette/content/atom.js");
const {
InvalidArgumentError,
InvalidSelectorError,
NoSuchElementError,
StaleElementReferenceError,
@ -1245,7 +1246,7 @@ class WebElement {
* @return {(ContentWebElement|ChromeWebElement)}
* Web element reference for <var>el</var>.
*
* @throws {TypeError}
* @throws {InvalidArgumentError}
* If <var>node</var> is neither a <code>WindowProxy</code>,
* DOM element, or a XUL element.
*/
@ -1263,7 +1264,7 @@ class WebElement {
return new ChromeWebElement(uuid);
}
throw new TypeError("Expected DOM window/element " +
throw new InvalidArgumentError("Expected DOM window/element " +
pprint`or XUL element, got: ${node}`);
}
@ -1280,16 +1281,12 @@ class WebElement {
* @return {WebElement}
* Representation of the web element.
*
* @throws {TypeError}
* @throws {InvalidArgumentError}
* If <var>json</var> is not a web element reference.
*/
static fromJSON(json) {
let keys = [];
try {
keys = Object.keys(json);
} catch (e) {
throw new TypeError(`Expected JSON Object: ${e}`);
}
assert.object(json);
let keys = Object.keys(json);
for (let key of keys) {
switch (key) {
@ -1308,7 +1305,7 @@ class WebElement {
}
}
throw new TypeError(
throw new InvalidArgumentError(
pprint`Expected web element reference, got: ${json}`);
}
@ -1333,9 +1330,8 @@ class WebElement {
* based on <var>context</var>.
*
* @throws {InvalidArgumentError}
* If <var>uuid</var> is not a string.
* @throws {TypeError}
* If <var>context</var> is an invalid context.
* If <var>uuid</var> is not a string or <var>context</var>
* is an invalid context.
*/
static fromUUID(uuid, context) {
assert.string(uuid);
@ -1348,7 +1344,7 @@ class WebElement {
return new ContentWebElement(uuid);
default:
throw new TypeError("Unknown context: " + context);
throw new InvalidArgumentError("Unknown context: " + context);
}
}
@ -1407,7 +1403,7 @@ class ContentWebElement extends WebElement {
const {Identifier, LegacyIdentifier} = ContentWebElement;
if (!(Identifier in json) && !(LegacyIdentifier in json)) {
throw new TypeError(
throw new InvalidArgumentError(
pprint`Expected web element reference, got: ${json}`);
}
@ -1434,7 +1430,7 @@ class ContentWebWindow extends WebElement {
static fromJSON(json) {
if (!(ContentWebWindow.Identifier in json)) {
throw new TypeError(
throw new InvalidArgumentError(
pprint`Expected web window reference, got: ${json}`);
}
let uuid = json[ContentWebWindow.Identifier];
@ -1459,7 +1455,8 @@ class ContentWebFrame extends WebElement {
static fromJSON(json) {
if (!(ContentWebFrame.Identifier in json)) {
throw new TypeError(pprint`Expected web frame reference, got: ${json}`);
throw new InvalidArgumentError(
pprint`Expected web frame reference, got: ${json}`);
}
let uuid = json[ContentWebFrame.Identifier];
return new ContentWebFrame(uuid);
@ -1482,7 +1479,7 @@ class ChromeWebElement extends WebElement {
static fromJSON(json) {
if (!(ChromeWebElement.Identifier in json)) {
throw new TypeError("Expected chrome element reference " +
throw new InvalidArgumentError("Expected chrome element reference " +
pprint`for XUL/XBL element, got: ${json}`);
}
let uuid = json[ChromeWebElement.Identifier];

Просмотреть файл

@ -12,6 +12,7 @@ const {
element,
WebElement,
} = Cu.import("chrome://marionette/content/element.js", {});
const {InvalidArgumentError} = Cu.import("chrome://marionette/content/error.js", {});
const SVGNS = "http://www.w3.org/2000/svg";
const XBLNS = "http://www.mozilla.org/xbl";
@ -245,7 +246,7 @@ add_test(function test_WebElement_from() {
ok(WebElement.from(domFrame) instanceof ContentWebFrame);
ok(WebElement.from(xulEl) instanceof ChromeWebElement);
Assert.throws(() => WebElement.from({}), /Expected DOM window\/element/);
Assert.throws(() => WebElement.from({}), InvalidArgumentError);
run_next_test();
});
@ -318,8 +319,8 @@ add_test(function test_WebElement_fromJSON_ChromeWebElement() {
});
add_test(function test_WebElement_fromJSON_malformed() {
Assert.throws(() => WebElement.fromJSON({}), /Expected web element reference/);
Assert.throws(() => WebElement.fromJSON(null), /Expected JSON Object/);
Assert.throws(() => WebElement.fromJSON({}), InvalidArgumentError);
Assert.throws(() => WebElement.fromJSON(null), InvalidArgumentError);
run_next_test();
});
@ -332,7 +333,7 @@ add_test(function test_WebElement_fromUUID() {
ok(domWebEl instanceof ContentWebElement);
equal(domWebEl.uuid, "bar");
Assert.throws(() => WebElement.fromUUID("baz", "bah"), /Unknown context/);
Assert.throws(() => WebElement.fromUUID("baz", "bah"), InvalidArgumentError);
run_next_test();
});
@ -389,7 +390,7 @@ add_test(function test_ContentWebElement_fromJSON() {
ok(bothEl instanceof ContentWebElement);
equal(bothEl.uuid, "identifier-uuid");
Assert.throws(() => ContentWebElement.fromJSON({}), /Expected web element reference/);
Assert.throws(() => ContentWebElement.fromJSON({}), InvalidArgumentError);
run_next_test();
});