Bug 1407778 - Added `status` Error property on Sqlite.openConnection. r=mak
authorZack Noyes, William Loughton <zacknoyes2@gmail.com>
Tue, 23 Jan 2018 23:11:54 -0800
changeset 453162 8f3552d6162728fffd709a25d8600acb06a69427
parent 453161 a29e9dbb8c42f927889b8f755e8c5e137920cdb1
child 453163 b19b1b50594e7e9d075e9dcb27bc88fe25850c95
push id8799
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 16:46:23 +0000
treeherdermozilla-beta@15334014dc67 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1407778
milestone60.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 1407778 - Added `status` Error property on Sqlite.openConnection. r=mak
toolkit/modules/Sqlite.jsm
toolkit/modules/tests/xpcshell/corrupt.sqlite
toolkit/modules/tests/xpcshell/test_sqlite.js
toolkit/modules/tests/xpcshell/xpcshell.ini
--- a/toolkit/modules/Sqlite.jsm
+++ b/toolkit/modules/Sqlite.jsm
@@ -940,17 +940,19 @@ function openConnection(options) {
       dbOptions.setProperty("readOnly", true);
     }
 
     dbOptions = dbOptions.enumerator.hasMoreElements() ? dbOptions : null;
 
     Services.storage.openAsyncDatabase(file, dbOptions, (status, connection) => {
       if (!connection) {
         log.warn(`Could not open connection to ${path}: ${status}`);
-        reject(new Error(`Could not open connection to ${path}: ${status}`));
+        let error = new Error(`Could not open connection to ${path}: ${status}`);
+        error.status = status;
+        reject(error);
         return;
       }
       log.info("Connection opened");
       try {
         resolve(
           new OpenedConnection(connection.QueryInterface(Ci.mozIStorageAsyncConnection),
                                identifier, openedOptions));
       } catch (ex) {
new file mode 100644
--- /dev/null
+++ b/toolkit/modules/tests/xpcshell/corrupt.sqlite
@@ -0,0 +1,1 @@
+CORRUPTED_SQLITE_FILE_CONTENTS
--- a/toolkit/modules/tests/xpcshell/test_sqlite.js
+++ b/toolkit/modules/tests/xpcshell/test_sqlite.js
@@ -1,11 +1,11 @@
 "use strict";
 
-var {classes: Cc, interfaces: Ci, utils: Cu} = Components;
+var {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 do_get_profile();
 
 Cu.import("resource://gre/modules/Promise.jsm");
 Cu.import("resource://gre/modules/PromiseUtils.jsm");
 Cu.import("resource://gre/modules/osfile.jsm");
 Cu.import("resource://gre/modules/FileUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
@@ -86,16 +86,34 @@ add_task(async function test_setup() {
   initTestLogging("Trace");
 });
 
 add_task(async function test_open_normal() {
   let c = await Sqlite.openConnection({path: "test_open_normal.sqlite"});
   await c.close();
 });
 
+add_task(async function test_open_normal_error() {
+  let currentDir = await OS.File.getCurrentDirectory();
+
+  let src = OS.Path.join(currentDir, "corrupt.sqlite");
+  Assert.ok((await OS.File.exists(src)), "Database file found");
+
+  // Ensure that our database doesn't already exist.
+  let path = OS.Path.join(OS.Constants.Path.profileDir, "corrupt.sqlite");
+  Assert.ok(!(await OS.File.exists(path)), "Database file should not exist yet");
+
+  await OS.File.copy(src, path);
+
+  let openPromise = Sqlite.openConnection({path});
+  await Assert.rejects(openPromise, reason => {
+    return reason.status == Cr.NS_ERROR_FILE_CORRUPTED;
+  }, "Check error status");
+});
+
 add_task(async function test_open_unshared() {
   let path = OS.Path.join(OS.Constants.Path.profileDir, "test_open_unshared.sqlite");
 
   let c = await Sqlite.openConnection({path, sharedMemoryCache: false});
   await c.close();
 });
 
 add_task(async function test_get_dummy_database() {
--- a/toolkit/modules/tests/xpcshell/xpcshell.ini
+++ b/toolkit/modules/tests/xpcshell/xpcshell.ini
@@ -1,14 +1,15 @@
 [DEFAULT]
 head = head.js
 support-files =
   propertyLists/bug710259_propertyListBinary.plist
   propertyLists/bug710259_propertyListXML.plist
   chromeappsstore.sqlite
+  corrupt.sqlite
   zips/zen.zip
 
 [test_BinarySearch.js]
 skip-if = toolkit == 'android'
 [test_CanonicalJSON.js]
 [test_client_id.js]
 skip-if = toolkit == 'android'
 [test_Color.js]