Browse Source

trap NS_ERROR_UNEXPECTED when calling reset() so callers can unconditionally reset a pref without having to check first that it exists and is in a non-default state (or trap an exception after the fact

Myk Melez 15 years ago
parent
commit
630f7a1ad3
2 changed files with 21 additions and 1 deletions
  1. 14 1
      Preferences.js
  2. 7 0
      test/unit/test_Preferences.js

+ 14 - 1
Preferences.js

@@ -138,7 +138,20 @@ Preferences.prototype = {
   },
 
   reset: function(prefName) {
-    this._prefSvc.clearUserPref(prefName);
+    try {
+      this._prefSvc.clearUserPref(prefName);
+    }
+    catch(ex) {
+      // The pref service throws NS_ERROR_UNEXPECTED when the caller tries
+      // to reset a pref that doesn't exist or is already set to its default
+      // value.  This interface fails silently in those cases, so callers
+      // can unconditionally reset a pref without having to check if it needs
+      // resetting first or trap exceptions after the fact.  It passes through
+      // other exceptions, however, so callers know about them, since we don't
+      // know what other exceptions might be thrown and what they might mean.
+      if (ex.result != Components.results.NS_ERROR_UNEXPECTED)
+        throw ex;
+    }
   },
 
   resetBranch: function(prefBranch) {

+ 7 - 0
test/unit/test_Preferences.js

@@ -0,0 +1,7 @@
+Components.utils.import("resource://jsmodules/Preferences.js");
+
+// Make sure the module doesn't throw an exception when asked to reset
+// a nonexistent pref.
+function test_reset_nonexistent_pref() {
+  Preferences.reset("nonexistent.pref");
+}