Bug 1420577 - Improve failure messages for invalid proxy configurations. draft
authorHenrik Skupin <mail@hskupin.info>
Mon, 04 Dec 2017 23:32:44 +0100
changeset 707374 b89d1f7fa36917b69c5b5f38e83aa7740f663a8d
parent 707373 ea8fa67480452c1e628b0917afbeac1d65b1bda7
child 742926 0ee68c224beadcc3ce0659cfb3dd830849b92f1f
push id92099
push userbmo:hskupin@gmail.com
push dateTue, 05 Dec 2017 07:09:47 +0000
bugs1420577
milestone59.0a1
Bug 1420577 - Improve failure messages for invalid proxy configurations. MozReview-Commit-ID: 4HitwxCUQH
testing/marionette/session.js
--- a/testing/marionette/session.js
+++ b/testing/marionette/session.js
@@ -7,17 +7,22 @@
 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
 Cu.importGlobalProperties(["URL"]);
 
 Cu.import("resource://gre/modules/Preferences.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
 Cu.import("chrome://marionette/content/assert.js");
-const {InvalidArgumentError} = Cu.import("chrome://marionette/content/error.js", {});
+const {
+  InvalidArgumentError,
+} = Cu.import("chrome://marionette/content/error.js", {});
+const {
+  pprint,
+} = Cu.import("chrome://marionette/content/format.js", {});
 
 this.EXPORTED_SYMBOLS = ["session"];
 
 // Enable testing this module, as Services.appinfo.* is not available
 // in xpcshell tests.
 const appinfo = {name: "<missing>", version: "<missing>"};
 try { appinfo.name = Services.appinfo.name.toLowerCase(); } catch (e) {}
 try { appinfo.version = Services.appinfo.version; } catch (e) {}
@@ -197,17 +202,18 @@ session.Proxy = class {
    */
   static fromJSON(json) {
     function stripBracketsFromIpv6Hostname(hostname) {
       return hostname.includes(":") ? hostname.replace(/[\[\]]/g, "") : hostname;
     }
 
     // Parse hostname and optional port from host
     function fromHost(scheme, host) {
-      assert.string(host);
+      assert.string(host,
+          pprint`Expected proxy "host" to be a string, got ${host}`);
 
       if (host.includes("://")) {
         throw new InvalidArgumentError(`${host} contains a scheme`);
       }
 
       let url;
       try {
         // To parse the host a scheme has to be added temporarily.
@@ -248,29 +254,33 @@ session.Proxy = class {
       return [hostname, port];
     }
 
     let p = new session.Proxy();
     if (typeof json == "undefined" || json === null) {
       return p;
     }
 
-    assert.object(json);
+    assert.object(json, pprint`Expected "proxy" to be an object, got ${json}`);
 
-    assert.in("proxyType", json);
-    p.proxyType = assert.string(json.proxyType);
+    assert.in("proxyType", json,
+        pprint`Expected "proxyType" in "proxy" object, got ${json}`);
+    p.proxyType = assert.string(json.proxyType,
+        pprint`Expected "proxyType" to be a string, got ${json.proxyType}`);
 
     switch (p.proxyType) {
       case "autodetect":
       case "direct":
       case "system":
         break;
 
       case "pac":
-        p.proxyAutoconfigUrl = assert.string(json.proxyAutoconfigUrl);
+        p.proxyAutoconfigUrl = assert.string(json.proxyAutoconfigUrl,
+            `Expected "proxyAutoconfigUrl" to be a string, ` +
+            pprint`got ${json.proxyAutoconfigUrl}`);
         break;
 
       case "manual":
         if (typeof json.ftpProxy != "undefined") {
           [p.ftpProxy, p.ftpProxyPort] = fromHost("ftp", json.ftpProxy);
         }
         if (typeof json.httpProxy != "undefined") {
           [p.httpProxy, p.httpProxyPort] = fromHost("http", json.httpProxy);
@@ -278,19 +288,21 @@ session.Proxy = class {
         if (typeof json.sslProxy != "undefined") {
           [p.sslProxy, p.sslProxyPort] = fromHost("https", json.sslProxy);
         }
         if (typeof json.socksProxy != "undefined") {
           [p.socksProxy, p.socksProxyPort] = fromHost("socks", json.socksProxy);
           p.socksVersion = assert.positiveInteger(json.socksVersion);
         }
         if (typeof json.noProxy != "undefined") {
-          let entries = assert.array(json.noProxy);
+          let entries = assert.array(json.noProxy,
+              pprint`Expected "noProxy" to be an array, got ${json.noProxy}`);
           p.noProxy = entries.map(entry => {
-            assert.string(entry);
+            assert.string(entry,
+                pprint`Expected "noProxy" entry to be a string, got ${entry}`);
             return stripBracketsFromIpv6Hostname(entry);
           });
         }
         break;
 
       default:
         throw new InvalidArgumentError(
             `Invalid type of proxy: ${p.proxyType}`);