Bug 918364 - Make exceptions more meaningful in OS.File. r=froydnj
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Fri, 11 Oct 2013 11:50:52 -0400
changeset 164303 012fc96d11a00b5885410472c1c6148c2f3e928f
parent 164302 39b5782426c56454690174c4fb68be891db2042c
child 164304 5836dc9eaa656426be9771e30b3769b518c243a6
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs918364
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 918364 - Make exceptions more meaningful in OS.File. r=froydnj
toolkit/components/osfile/modules/osfile_async_front.jsm
toolkit/components/osfile/tests/xpcshell/test_exception.js
toolkit/components/osfile/tests/xpcshell/xpcshell.ini
--- a/toolkit/components/osfile/modules/osfile_async_front.jsm
+++ b/toolkit/components/osfile/modules/osfile_async_front.jsm
@@ -203,19 +203,26 @@ let Scheduler = {
           options.outExecutionDuration = durationMs;
         }
         return data.ok;
       },
       function onError(error) {
         // Decode any serialized error
         if (error instanceof PromiseWorker.WorkerError) {
           throw OS.File.Error.fromMsg(error.data);
-        } else {
-          throw error;
         }
+        // Extract something meaningful from WorkerErrorEvent
+        if (typeof error == "object" && error && error.constructor.name == "WorkerErrorEvent") {
+          let message = error.message;
+          if (message == "uncaught exception: [object StopIteration]") {
+            throw StopIteration;
+          }
+          throw new Error(message, error.filename, error.lineno);
+        }
+        throw error;
       }
     );
   }
 };
 
 const PREF_OSFILE_LOG = "toolkit.osfile.log";
 const PREF_OSFILE_LOG_REDIRECT = "toolkit.osfile.log.redirect";
 
@@ -960,24 +967,21 @@ DirectoryIterator.prototype = {
     if (this._isClosed) {
       return this._itmsg;
     }
     let self = this;
     let promise = Scheduler.post("DirectoryIterator_prototype_next", [iterator]);
     promise = promise.then(
       DirectoryIterator.Entry.fromMsg,
       function onReject(reason) {
-        // If the exception is |StopIteration| (which we may determine only
-        // from its message...) we need to stop the iteration.
-        if (!(reason instanceof WorkerErrorEvent && reason.message == "uncaught exception: [object StopIteration]")) {
-          // Any exception other than StopIteration should be propagated as such
-          throw reason;
+        if (reason == StopIteration) {
+          self.close();
+          throw StopIteration;
         }
-        self.close();
-        throw StopIteration;
+        throw reason;
       });
     return promise;
   },
   /**
    * Close the iterator
    */
   close: function close() {
     if (this._isClosed) {
new file mode 100644
--- /dev/null
+++ b/toolkit/components/osfile/tests/xpcshell/test_exception.js
@@ -0,0 +1,29 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+Components.utils.import("resource://gre/modules/osfile.jsm");
+
+function run_test() {
+  do_test_pending();
+  run_next_test();
+}
+
+add_task(function test_typeerror() {
+  let exn;
+  try {
+    let fd = yield OS.File.open("/tmp", {no_such_key: 1});
+    do_print("Fd: " + fd);
+  } catch (ex) {
+    exn = ex;
+  }
+  do_print("Exception: " + exn);
+  do_check_true(typeof exn == "object");
+  do_check_true("name" in exn);
+  do_check_true(exn.message.indexOf("TypeError") != -1);
+});
+
+add_task(function() {
+  do_test_finished();
+});
--- a/toolkit/components/osfile/tests/xpcshell/xpcshell.ini
+++ b/toolkit/components/osfile/tests/xpcshell/xpcshell.ini
@@ -3,10 +3,11 @@ head =
 tail =
 
 [test_osfile_closed.js]
 [test_path.js]
 [test_osfile_async.js]
 [test_profiledir.js]
 [test_logging.js]
 [test_creationDate.js]
+[test_exception.js]
 [test_path_constants.js]
 [test_removeDir.js]