Bug 1428094 - Exception raised from a webRequest blocking listener should report the original error message and filename. draft
authorLuca Greco <lgreco@mozilla.com>
Thu, 07 Dec 2017 21:19:03 +0100
changeset 718552 23d1f069474b70293f4c7b420033362766afe882
parent 717738 6f5fac320fcb6625603fa8a744ffa8523f8b3d71
child 745518 35fb495f2ea5da15e4105933a02fbb14085a49ec
push id94963
push userluca.greco@alcacoop.it
push dateWed, 10 Jan 2018 13:49:34 +0000
bugs1428094
milestone59.0a1
Bug 1428094 - Exception raised from a webRequest blocking listener should report the original error message and filename. MozReview-Commit-ID: KiCg6dKaVd5
toolkit/components/extensions/test/mochitest/test_ext_webrequest_suspend.html
toolkit/modules/addons/WebRequest.jsm
--- a/toolkit/components/extensions/test/mochitest/test_ext_webrequest_suspend.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_webrequest_suspend.html
@@ -211,12 +211,73 @@ add_task(async function test_set_respons
 
   let headerValue = await chromeScript.promiseOneMessage("response-header-foo");
   is(headerValue, "bar", "Expected Foo header value");
 
   await extension.unload();
   chromeScript.destroy();
 });
 
+// Test that exceptions raised from a blocking webRequest listener that returns
+// a promise are logged as expected.
+add_task(async function test_logged_error_on_promise_result() {
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: [
+        "webRequest",
+        "webRequestBlocking",
+        "http://mochi.test/*",
+      ],
+    },
+
+    background() {
+      async function onBeforeRequest() {
+        throw new Error("Expected webRequest exception from a promise result");
+      }
+
+      let exceptionRaised = false;
+
+      browser.webRequest.onBeforeRequest.addListener(() => {
+        if (exceptionRaised) {
+          return;
+        }
+
+        // We only need to raise the exception once.
+        exceptionRaised = true;
+        return onBeforeRequest();
+      }, {
+        urls: ["http://mochi.test/*"],
+        types: ["main_frame"],
+      }, ["blocking"]);
+
+      browser.webRequest.onBeforeRequest.addListener(() => {
+        browser.test.sendMessage("web-request-event-received");
+      }, {
+        urls: ["http://mochi.test/*"],
+        types: ["main_frame"],
+      }, ["blocking"]);
+
+      browser.test.sendMessage("background-ready");
+    },
+  });
+
+  // Start to monitor the console service for the expected console message.
+  consoleMonitor.start([{message: /Expected webRequest exception from a promise result/}]);
+
+  await extension.startup();
+
+  await extension.awaitMessage("background-ready");
+
+  const testWin = window.open("http://mochi.test:8888/", "_blank", "width=100,height=100");
+  await waitForLoad(testWin);
+  await extension.awaitMessage("web-request-event-received");
+  testWin.close();
+
+  // Check that the collected messages contains the expected console message, and fails
+  // otherwise.
+  await consoleMonitor.finished();
+
+  await extension.unload();
+});
 
 </script>
 </body>
 </html>
--- a/toolkit/modules/addons/WebRequest.jsm
+++ b/toolkit/modules/addons/WebRequest.jsm
@@ -798,17 +798,25 @@ HttpObserverManager = {
 
     try {
       for (let {opts, result} of handlerResults) {
         if (isThenable(result)) {
           channel.suspended = true;
           try {
             result = await result;
           } catch (e) {
-            Cu.reportError(e);
+            let error;
+
+            if (e instanceof Error) {
+              error = e;
+            } else if (typeof e === "object" && e.message) {
+              error = new Error(e.message, e.fileName, e.lineNumber);
+            }
+
+            Cu.reportError(error);
             continue;
           }
           if (!result || typeof result !== "object") {
             continue;
           }
         }
 
         if (kind === "authRequired" && result.authCredentials && channel.authPromptCallback) {