Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updating goog.db to comply with the latest specs regarding error repo…

…rting.

See http://www.w3.org/TR/IndexedDB/#exceptions

Errors used to be reported as numerical error codes. Now they are reported
as strings. Chrome25 is the first version that is not backward compatible.
Chrome 22, 23, 24 support both.

R=nweiz
DELTA=328 (215 added, 20 deleted, 93 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=6247


git-svn-id: http://closure-library.googlecode.com/svn/trunk@2501 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information...
commit 53996fb0c6f14eba3df063ac0202e6d65f9138ea 1 parent 3b34e61
dpapad@google.com authored
View
10 closure/goog/db/cursor.js
@@ -86,7 +86,7 @@ goog.db.Cursor.prototype.update = function(value) {
request = this.cursor_.update(value);
} catch (err) {
msg += goog.debug.deepExpose(value);
- d.errback(goog.db.Error.create(err, msg));
+ d.errback(goog.db.Error.fromException(err, msg));
return d;
}
request.onsuccess = function(ev) {
@@ -94,8 +94,7 @@ goog.db.Cursor.prototype.update = function(value) {
};
request.onerror = function(ev) {
msg += goog.debug.deepExpose(value);
- d.errback(new goog.db.Error(
- (/** @type {IDBRequest} */ (ev.target)).errorCode, msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
return d;
};
@@ -115,15 +114,14 @@ goog.db.Cursor.prototype.remove = function() {
try {
request = this.cursor_['delete']();
} catch (err) {
- d.errback(goog.db.Error.create(err, msg));
+ d.errback(goog.db.Error.fromException(err, msg));
return d;
}
request.onsuccess = function(ev) {
d.callback();
};
request.onerror = function(ev) {
- d.errback(new goog.db.Error(
- (/** @type {IDBRequest} */ (ev.target)).errorCode, msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
return d;
};
View
4 closure/goog/db/db.js
@@ -133,7 +133,7 @@ goog.db.openDatabase = function(name, opt_version, opt_onUpgradeNeeded,
};
openRequest.onerror = function(ev) {
var msg = 'opening database ' + name;
- d.errback(new goog.db.Error(ev.target.errorCode, msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
openRequest.onupgradeneeded = function(ev) {
if (!opt_onUpgradeNeeded) return;
@@ -170,7 +170,7 @@ goog.db.deleteDatabase = function(name, opt_onBlocked) {
};
deleteRequest.onerror = function(ev) {
var msg = 'deleting database ' + name;
- d.errback(new goog.db.Error(ev.target.errorCode, msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
deleteRequest.onblocked = function(ev) {
if (opt_onBlocked) {
View
143 closure/goog/db/db_test.html
@@ -14,6 +14,7 @@
<script>
goog.require('goog.array');
goog.require('goog.async.Deferred');
+goog.require('goog.async.DeferredList');
goog.require('goog.db');
goog.require('goog.db.IndexedDb');
goog.require('goog.db.KeyRange');
@@ -133,7 +134,7 @@
fail('Create transaction with a non-existent store should have failed.');
} catch (e) {
// expected
- assertEquals(e.code, goog.db.Error.ErrorCode.NOT_FOUND_ERR);
+ assertEquals(e.getName(), goog.db.Error.ErrorName.NOT_FOUND_ERR);
closeAndContinue(db);
}
}
@@ -240,14 +241,14 @@
}
asyncTestCase.waitForAsync('bad object store manipulation');
- var expectedCode = goog.db.Error.ErrorCode.INVALID_STATE_ERR;
+ var expectedCode = goog.db.Error.ErrorName.INVALID_STATE_ERR;
globalDb.branch().addCallback(function(db) {
try {
db.createObjectStore('diediedie');
fail('Create object store outside transaction should have failed.');
} catch (err) {
// expected
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
}
}).addCallback(addStore).addCallback(function(db) {
try {
@@ -255,7 +256,7 @@
fail('Delete object store outside transaction should have failed.');
} catch (err) {
// expected
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
}
}).addCallback(function(db) {
return incrementVersion(db, function(ev, db, tx) {
@@ -264,7 +265,7 @@
fail('Delete non-existent store should have failed.');
} catch (err) {
// expected
- assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName());
}
});
}).addCallback(closeAndContinue).addErrback(failOnError);
@@ -282,7 +283,7 @@
tx.objectStore('diediedie');
fail('getting non-existent object store should have failed');
} catch (err) {
- assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName());
}
closeAndContinue(db);
}).addErrback(failOnError);
@@ -404,8 +405,8 @@
function(err) {
// expected
assertEquals(
- goog.db.Error.ErrorCode.CONSTRAINT_ERR,
- err.code);
+ goog.db.Error.ErrorName.CONSTRAINT_ERR,
+ err.getName());
});
goog.events.listen(addOverwriteTx, COMPLETE, function() {
@@ -414,8 +415,8 @@
goog.events.listen(addOverwriteTx, ERROR, function(ev) {
// expected
assertEquals(
- goog.db.Error.ErrorCode.CONSTRAINT_ERR,
- ev.target.code);
+ goog.db.Error.ErrorName.CONSTRAINT_ERR,
+ ev.target.getName());
closeAndContinue(db);
});
});
@@ -480,7 +481,7 @@
fail('inserting with explicit key should have failed');
}).addErrback(function(err) {
// expected
- assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.DATA_ERR, err.getName());
closeAndContinue(db);
});
}).addErrback(failOnError);
@@ -572,19 +573,19 @@
tx.objectStore('store').put('death', null).addCallback(function() {
fail('putting with null key should have failed');
}).addErrback(function(err) {
- assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.DATA_ERR, err.getName());
});
tx.objectStore('store').put('death', NaN).addCallback(function() {
fail('putting with NaN key should have failed');
}).addErrback(function(err) {
- assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.DATA_ERR, err.getName());
});
tx.objectStore('store').put('death', undefined).addCallback(function() {
fail('putting with undefined key should have failed');
}).addErrback(function(err) {
- assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.DATA_ERR, err.getName());
});
closeAndContinue(db);
@@ -716,9 +717,18 @@
var openCursorAndCheck = function(db) {
var cursorTx = db.createTransaction(['store']);
var store = cursorTx.objectStore('store');
- var d = new goog.async.Deferred();
var values = [];
var keys = [];
+
+ var whenTxComplete = new goog.async.Deferred();
+ goog.events.listen(
+ cursorTx, goog.db.Transaction.EventTypes.ERROR, failOnError);
+ goog.events.listen(
+ cursorTx, goog.db.Transaction.EventTypes.COMPLETE, function() {
+ whenTxComplete.callback();
+ });
+
+ var whenCursorComplete = new goog.async.Deferred();
var cursor = store.openCursor(
goog.db.KeyRange.bound('b', 'c'),
goog.db.Cursor.Direction.PREV);
@@ -736,13 +746,18 @@
], function(evt) {
goog.events.unlistenByKey(key);
if (evt.type == goog.db.Cursor.EventType.COMPLETE) {
- d.callback(db);
+ whenCursorComplete.callback(db);
} else {
- d.errback();
+ whenCursorComplete.errback();
}
});
- d.addCallback(function(db) {
+ var whenAllComplete = goog.async.DeferredList.gatherResults([
+ whenCursorComplete,
+ whenTxComplete
+ ]);
+ whenAllComplete.addCallback(function(results) {
+ var db = results[0];
assertArrayEquals(['3', '2'], values);
assertArrayEquals(['c', 'b'], keys);
closeAndContinue(db);
@@ -775,7 +790,17 @@
var cursorTx = db.createTransaction(
['store'],
goog.db.Transaction.TransactionMode.READ_WRITE);
+
+ var whenTxComplete = new goog.async.Deferred();
+ goog.events.listen(
+ cursorTx, goog.db.Transaction.EventTypes.ERROR, failOnError);
+ goog.events.listen(
+ cursorTx, goog.db.Transaction.EventTypes.COMPLETE, function() {
+ whenTxComplete.callback();
+ });
+
var store = cursorTx.objectStore('store');
+ var whenCursorComplete = new goog.async.Deferred();
var cursor = store.openCursor(goog.db.KeyRange.bound('b', 'c', true));
var key = goog.events.listen(
@@ -792,11 +817,19 @@
], function(evt) {
goog.events.unlistenByKey(key);
if (evt.type == goog.db.Cursor.EventType.COMPLETE) {
- d.callback(db);
+ whenCursorComplete.callback(db);
} else {
- d.errback();
+ whenCursorComplete.errback();
}
});
+
+ goog.async.DeferredList.gatherResults([
+ whenCursorComplete,
+ whenTxComplete
+ ]).addCallbacks(function(results) {
+ d.callback(results[0]);
+ }, failOnError);
+
return d;
};
@@ -828,7 +861,18 @@
var cursorTx = db.createTransaction(
['store'],
goog.db.Transaction.TransactionMode.READ_WRITE);
+
+ var whenTxComplete = new goog.async.Deferred();
+ goog.events.listen(
+ cursorTx, goog.db.Transaction.EventTypes.ERROR, failOnError);
+ goog.events.listen(
+ cursorTx, goog.db.Transaction.EventTypes.COMPLETE, function() {
+ whenTxComplete.callback();
+ });
+
+
var store = cursorTx.objectStore('store');
+ var whenCursorComplete = new goog.async.Deferred();
var cursor = store.openCursor(goog.db.KeyRange.lowerBound('b', true));
var key = goog.events.listen(
@@ -844,11 +888,19 @@
], function(evt) {
goog.events.unlistenByKey(key);
if (evt.type == goog.db.Cursor.EventType.COMPLETE) {
- d.callback(db);
+ whenCursorComplete.callback(db);
} else {
- d.errback();
+ whenCursorComplete.errback();
}
});
+
+ goog.async.DeferredList.gatherResults([
+ whenCursorComplete,
+ whenTxComplete
+ ]).addCallbacks(function(results) {
+ d.callback(results[0]);
+ }, failOnError);
+
return d;
};
@@ -957,14 +1009,14 @@
var index = store.getIndex('index');
store.put({key: 'something', value: 'anything'});
goog.events.listen(tx, goog.db.Transaction.EventTypes.COMPLETE, function() {
- var expectedCode = goog.db.Error.ErrorCode.TRANSACTION_INACTIVE_ERR;
+ var expectedCode = goog.db.Error.ErrorName.TRANSACTION_INACTIVE_ERR;
store.put({
key: 'another',
value: 'thing'
}).addCallback(function() {
fail('putting with inactive transaction should have failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
store.add({
key: 'another',
@@ -972,27 +1024,27 @@
}).addCallback(function() {
fail('adding with inactive transaction should have failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
store.remove('something').addCallback(function() {
fail('deleting with inactive transaction should have failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
store.get('something').addCallback(function() {
fail('getting with inactive transaction should have failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
store.getAll().addCallback(function() {
fail('getting all with inactive transaction should have failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
store.clear().addCallback(function() {
fail('clearing all with inactive transaction should have failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
index.get('anything').
@@ -1000,28 +1052,28 @@
fail('getting from index with inactive transaction should have '
+ 'failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
index.getKey('anything').
addCallback(function() {
fail('getting key from index with inactive transaction '
+ 'should have failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
index.getAll('anything').
addCallback(function() {
fail('getting all from index with inactive transaction '
+ 'should have failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
index.getAllKeys('anything').
addCallback(function() {
fail('getting all keys from index with inactive transaction '
+ 'should have failed');
}).addErrback(function(err) {
- assertEquals(expectedCode, err.code);
+ assertEquals(expectedCode, err.getName());
});
closeAndContinue(db);
});
@@ -1040,17 +1092,17 @@
tx.objectStore('store').put('KABOOM!', 'anything').addCallback(function() {
fail('putting should have failed');
}).addErrback(function(err) {
- assertEquals(goog.db.Error.ErrorCode.READ_ONLY_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.READ_ONLY_ERR, err.getName());
});
tx.objectStore('store').add('EXPLODE!', 'die').addCallback(function() {
fail('adding should have failed');
}).addErrback(function(err) {
- assertEquals(goog.db.Error.ErrorCode.READ_ONLY_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.READ_ONLY_ERR, err.getName());
});
tx.objectStore('store').remove('no key', 'nothing').addCallback(function() {
fail('deleting should have failed');
}).addErrback(function(err) {
- assertEquals(goog.db.Error.ErrorCode.READ_ONLY_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.READ_ONLY_ERR, err.getName());
});
closeAndContinue(db);
}).addErrback(failOnError);
@@ -1079,7 +1131,7 @@
var dies = store.getIndex('diediedie');
fail('getting non-existent index should have failed');
} catch (err) {
- assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName());
}
return tx.wait();
@@ -1091,7 +1143,7 @@
store.deleteIndex('diediedie');
fail('deleting non-existent index should have failed');
} catch (err) {
- assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName());
}
});
}).addCallback(function(db) {
@@ -1101,7 +1153,7 @@
var index = store.getIndex('index');
fail('getting deleted index should have failed');
} catch (err) {
- assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
+ assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName());
}
var uniqueIndex = store.getIndex('uniqueIndex');
var multirowIndex = store.getIndex('multirowIndex');
@@ -1220,14 +1272,11 @@
goog.events.listen(tx, goog.db.Transaction.EventTypes.ERROR, function(ev) {
// expected
assertTrue(
- 'Expected DATA_ERR, CONSTRAINT_ERR or UNKNOWN_ERR, was ' +
- ev.target.code,
- // Chrome 15-21, 23+
- ev.target.code == goog.db.Error.ErrorCode.CONSTRAINT_ERR ||
- // Chrome 22
- ev.target.code == goog.db.Error.ErrorCode.DATA_ERR ||
- // Chrome 14
- ev.target.code == goog.db.Error.ErrorCode.UNKNOWN_ERR);
+ "Expected DATA_ERR, CONSTRAINT_ERR, was ",
+ // Chrome 21, 23+.
+ goog.db.Error.ErrorName.CONSTRAINT_ERR == ev.target.getName() ||
+ // Chrome 22.
+ goog.db.Error.ErrorName.DATE_ERR == ev.target.getName());
closeAndContinue(db);
});
}).addErrback(failOnError);
View
188 closure/goog/db/error.js
@@ -20,6 +20,7 @@
goog.provide('goog.db.Error');
goog.provide('goog.db.Error.ErrorCode');
+goog.provide('goog.db.Error.ErrorName');
goog.provide('goog.db.Error.VersionChangeBlockedError');
goog.require('goog.debug.Error');
@@ -30,29 +31,56 @@ goog.require('goog.debug.Error');
* A database error. Since the stack trace can be unhelpful in an asynchronous
* context, the error provides a message about where it was produced.
*
- * @param {number} code The error code.
+ * @param {number|!DOMError} error The DOMError instance returned by the
+ * browser for Chrome22+, or an error code for previous versions.
* @param {string} context A description of where the error occured.
* @param {string=} opt_message Additional message.
* @constructor
* @extends {goog.debug.Error}
*/
-goog.db.Error = function(code, context, opt_message) {
- var msg = 'Error ' + context + ': ' + goog.db.Error.getMessage(code);
- if (opt_message) {
- msg += ', ' + opt_message;
+goog.db.Error = function(error, context, opt_message) {
+ var errorCode = null;
+ var internalError = null;
+ if (goog.isNumber(error)) {
+ errorCode = error;
+ internalError = {name: goog.db.Error.getName(errorCode)};
+ } else {
+ internalError = error;
+ errorCode = goog.db.Error.getCode(error.name);
}
- goog.base(this, msg);
/**
* The code for this error.
*
* @type {number}
*/
- this.code = code;
+ this.code = errorCode;
+
+ /**
+ * The DOMException as returned by the browser.
+ *
+ * @type {!DOMError}
+ * @private
+ */
+ this.error_ = /** @type {!DOMError} */ (internalError);
+
+ var msg = 'Error ' + context + ': ' + this.getName();
+ if (opt_message) {
+ msg += ', ' + opt_message;
+ }
+ goog.base(this, msg);
};
goog.inherits(goog.db.Error, goog.debug.Error);
+/**
+ * @return {string} The name of the error.
+ */
+goog.db.Error.prototype.getName = function() {
+ return this.error_.name;
+};
+
+
/**
* A specific kind of database error. If a Version Change is unable to proceed
@@ -184,14 +212,146 @@ goog.db.Error.getMessage = function(code) {
/**
- * Returns an error, wrapping it in a {@link goog.db.Error} if it's an IndexedDB
- * error.
+ * Names of all possible errors as returned from the browser.
+ * @see http://www.w3.org/TR/IndexedDB/#exceptions
+ * @enum {string}
+ */
+goog.db.Error.ErrorName = {
+ ABORT_ERR: 'AbortError',
+ CONSTRAINT_ERR: 'ConstraintError',
+ DATA_CLONE_ERR: 'DataCloneError',
+ DATA_ERR: 'DataError',
+ INVALID_ACCESS_ERR: 'InvalidAccessError',
+ INVALID_STATE_ERR: 'InvalidStateError',
+ NOT_FOUND_ERR: 'NotFoundError',
+ QUOTA_EXCEEDED_ERR: 'QuotaExceededError',
+ READ_ONLY_ERR: 'ReadOnlyError',
+ SYNTAX_ERROR: 'SyntaxError',
+ TIMEOUT_ERR: 'TimeoutError',
+ TRANSACTION_INACTIVE_ERR: 'TransactionInactiveError',
+ UNKNOWN_ERR: 'UnknownError',
+ VERSION_ERR: 'VersionError'
+};
+
+
+/**
+ * Translates an error name to an error code. This is purely kept for backwards
+ * compatibility with Chrome21.
+ *
+ * @param {string} name The name of the erorr.
+ * @return {number} The error code corresponding to the error.
+ */
+goog.db.Error.getCode = function(name) {
+ switch (name) {
+ case goog.db.Error.ErrorName.UNKNOWN_ERR:
+ return goog.db.Error.ErrorCode.UNKNOWN_ERR;
+ case goog.db.Error.ErrorName.NOT_FOUND_ERR:
+ return goog.db.Error.ErrorCode.NOT_FOUND_ERR;
+ case goog.db.Error.ErrorName.CONSTRAINT_ERR:
+ return goog.db.Error.ErrorCode.CONSTRAINT_ERR;
+ case goog.db.Error.ErrorName.DATA_ERR:
+ return goog.db.Error.ErrorCode.DATA_ERR;
+ case goog.db.Error.ErrorName.TRANSACTION_INACTIVE_ERR:
+ return goog.db.Error.ErrorCode.TRANSACTION_INACTIVE_ERR;
+ case goog.db.Error.ErrorName.ABORT_ERR:
+ return goog.db.Error.ErrorCode.ABORT_ERR;
+ case goog.db.Error.ErrorName.READ_ONLY_ERR:
+ return goog.db.Error.ErrorCode.READ_ONLY_ERR;
+ case goog.db.Error.ErrorName.TIMEOUT_ERR:
+ return goog.db.Error.ErrorCode.TIMEOUT_ERR;
+ case goog.db.Error.ErrorName.QUOTA_EXCEEDED_ERR:
+ return goog.db.Error.ErrorCode.QUOTA_ERR;
+ case goog.db.Error.ErrorName.INVALID_ACCESS_ERR:
+ return goog.db.Error.ErrorCode.INVALID_ACCESS_ERR;
+ case goog.db.Error.ErrorName.INVALID_STATE_ERR:
+ return goog.db.Error.ErrorCode.INVALID_STATE_ERR;
+ default:
+ return goog.db.Error.ErrorCode.UNKNOWN_ERR;
+ }
+};
+
+
+/**
+ * Converts an error code used by the old spec, to an error name used by the
+ * latest spec.
+ * @see http://www.w3.org/TR/IndexedDB/#exceptions
*
- * @param {Error} err The error object to possibly wrap.
+ * @param {!goog.db.Error.ErrorCode|number} code The error code to convert.
+ * @return {!goog.db.Error.ErrorName} The corresponding name of the error.
+ */
+goog.db.Error.getName = function(code) {
+ switch (code) {
+ case goog.db.Error.ErrorCode.UNKNOWN_ERR:
+ return goog.db.Error.ErrorName.UNKNOWN_ERR;
+ case goog.db.Error.ErrorCode.NOT_FOUND_ERR:
+ return goog.db.Error.ErrorName.NOT_FOUND_ERR;
+ case goog.db.Error.ErrorCode.CONSTRAINT_ERR:
+ return goog.db.Error.ErrorName.CONSTRAINT_ERR;
+ case goog.db.Error.ErrorCode.DATA_ERR:
+ return goog.db.Error.ErrorName.DATA_ERR;
+ case goog.db.Error.ErrorCode.TRANSACTION_INACTIVE_ERR:
+ return goog.db.Error.ErrorName.TRANSACTION_INACTIVE_ERR;
+ case goog.db.Error.ErrorCode.ABORT_ERR:
+ return goog.db.Error.ErrorName.ABORT_ERR;
+ case goog.db.Error.ErrorCode.READ_ONLY_ERR:
+ return goog.db.Error.ErrorName.READ_ONLY_ERR;
+ case goog.db.Error.ErrorCode.TIMEOUT_ERR:
+ return goog.db.Error.ErrorName.TIMEOUT_ERR;
+ case goog.db.Error.ErrorCode.QUOTA_ERR:
+ return goog.db.Error.ErrorName.QUOTA_EXCEEDED_ERR;
+ case goog.db.Error.ErrorCode.INVALID_ACCESS_ERR:
+ return goog.db.Error.ErrorName.INVALID_ACCESS_ERR;
+ case goog.db.Error.ErrorCode.INVALID_STATE_ERR:
+ return goog.db.Error.ErrorName.INVALID_STATE_ERR;
+ default:
+ return goog.db.Error.ErrorName.UNKNOWN_ERR;
+ }
+};
+
+
+/**
+ * Constructs an goog.db.Error instance from an IDBRequest. This abstraction is
+ * necessary to provide backwards compatibility with Chrome21.
+ *
+ * @param {!IDBRequest} request The request that failed.
+ * @param {string} message The error message to add to err if it's wrapped.
+ * @return {!goog.db.Error} The error that caused the failure.
+ */
+goog.db.Error.fromRequest = function(request, message) {
+ if ('error' in request) {
+ // Chrome 21 and before.
+ return new goog.db.Error(request.error, message);
+ } else if ('name' in request) {
+ // Chrome 22+.
+ var errorName = goog.db.Error.getName(request.errorCode);
+ return new goog.db.Error(
+ /**@type {!DOMError} */ ({name: errorName}), message);
+ } else {
+ return new goog.db.Error(/** @type {!DOMError} */ (
+ {name: goog.db.Error.ErrorName.UNKNOWN_ERR}), message);
+ }
+};
+
+
+/**
+ * Constructs an goog.db.Error instance from an DOMException. This abstraction
+ * is necessary to provide backwards compatibility with Chrome21.
+ *
+ * @param {!IDBDatabaseException} ex The exception that was thrown.
* @param {string} message The error message to add to err if it's wrapped.
- * @return {goog.db.Error|Error} The possibly-wrapped error.
+ * @return {!goog.db.Error} The error that caused the failure.
*/
-goog.db.Error.create = function(err, message) {
- if (!('code' in err)) return err;
- return new goog.db.Error(err.code, message);
+goog.db.Error.fromException = function(ex, message) {
+ if ('name' in ex) {
+ // Chrome 21 and before.
+ return new goog.db.Error(/** @type {!DOMError} */ (ex), message);
+ } else if ('code' in ex) {
+ // Chrome 22+.
+ var errorName = goog.db.Error.getName(ex.code);
+ return new goog.db.Error(
+ /** @type {!DOMError} */ ({name: errorName}), message);
+ } else {
+ return new goog.db.Error(/** @type {!DOMError} */ (
+ {name: goog.db.Error.ErrorName.UNKNOWN_ERR}), message);
+ }
};
View
12 closure/goog/db/index.js
@@ -88,7 +88,7 @@ goog.db.Index.prototype.get_ = function(fn, msg, key) {
request = this.index_[fn](key);
} catch (err) {
msg += ' with key ' + goog.debug.deepExpose(key);
- d.errback(goog.db.Error.create(err, msg));
+ d.errback(goog.db.Error.fromException(err, msg));
return d;
}
request.onsuccess = function(ev) {
@@ -96,9 +96,7 @@ goog.db.Index.prototype.get_ = function(fn, msg, key) {
};
request.onerror = function(ev) {
msg += ' with key ' + goog.debug.deepExpose(key);
- d.errback(new goog.db.Error(
- (/** @type {IDBRequest} */ (ev.target)).errorCode,
- msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
return d;
};
@@ -155,7 +153,7 @@ goog.db.Index.prototype.getAll_ = function(fn, msg, opt_key) {
if (opt_key) {
msg += ' for key ' + goog.debug.deepExpose(opt_key);
}
- d.errback(goog.db.Error.create(err, msg));
+ d.errback(goog.db.Error.fromException(err, msg));
return d;
}
var result = [];
@@ -172,9 +170,7 @@ goog.db.Index.prototype.getAll_ = function(fn, msg, opt_key) {
if (opt_key) {
msg += ' for key ' + goog.debug.deepExpose(opt_key);
}
- d.errback(new goog.db.Error(
- (/** @type {IDBRequest} */ (ev.target)).errorCode,
- msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
return d;
};
View
10 closure/goog/db/indexeddb.js
@@ -180,7 +180,7 @@ goog.db.IndexedDb.prototype.createObjectStore = function(name, opt_params) {
return new goog.db.ObjectStore(this.db_.createObjectStore(
name, opt_params));
} catch (ex) {
- throw goog.db.Error.create(ex, 'creating object store ' + name);
+ throw goog.db.Error.fromException(ex, 'creating object store ' + name);
}
};
@@ -197,7 +197,7 @@ goog.db.IndexedDb.prototype.deleteObjectStore = function(name) {
try {
this.db_.deleteObjectStore(name);
} catch (ex) {
- throw goog.db.Error.create(ex, 'deleting object store ' + name);
+ throw goog.db.Error.fromException(ex, 'deleting object store ' + name);
}
};
@@ -232,7 +232,7 @@ goog.db.IndexedDb.prototype.setVersion = function(version) {
// If a version change is blocked, onerror and onblocked may both fire.
// Check d.hasFired() to avoid an AlreadyCalledError.
if (!d.hasFired()) {
- d.errback(new goog.db.Error(ev.target.errorCode, 'setting version'));
+ d.errback(goog.db.Error.fromRequest(ev.target, 'setting version'));
}
};
request.onblocked = function(ev) {
@@ -266,8 +266,8 @@ goog.db.IndexedDb.prototype.createTransaction = function(storeNames, opt_mode) {
this.db_.transaction(storeNames, opt_mode) :
this.db_.transaction(storeNames);
return new goog.db.Transaction(transaction, this);
- } catch (err) {
- throw goog.db.Error.create(err, 'creating transaction');
+ } catch (ex) {
+ throw goog.db.Error.fromException(ex, 'creating transaction');
}
};
View
42 closure/goog/db/objectstore.js
@@ -85,12 +85,12 @@ goog.db.ObjectStore.prototype.insert_ = function(fn, msg, value, opt_key) {
} else {
request = this.store_[fn](value);
}
- } catch (err) {
+ } catch (ex) {
msg += goog.debug.deepExpose(value);
if (opt_key) {
msg += ', with key ' + goog.debug.deepExpose(opt_key);
}
- d.errback(goog.db.Error.create(err, msg));
+ d.errback(goog.db.Error.fromException(ex, msg));
return d;
}
request.onsuccess = function(ev) {
@@ -102,9 +102,7 @@ goog.db.ObjectStore.prototype.insert_ = function(fn, msg, value, opt_key) {
if (opt_key) {
msg += ', with key ' + goog.debug.deepExpose(opt_key);
}
- d.errback(new goog.db.Error(
- (/** @type {IDBRequest} */ (ev.target)).errorCode,
- msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
return d;
};
@@ -163,7 +161,7 @@ goog.db.ObjectStore.prototype.remove = function(key) {
} catch (err) {
var msg = 'removing from ' + this.getName() + ' with key ' +
goog.debug.deepExpose(key);
- d.errback(goog.db.Error.create(err, msg));
+ d.errback(goog.db.Error.fromException(err, msg));
return d;
}
request.onsuccess = function(ev) {
@@ -173,9 +171,7 @@ goog.db.ObjectStore.prototype.remove = function(key) {
request.onerror = function(ev) {
var msg = 'removing from ' + self.getName() + ' with key ' +
goog.debug.deepExpose(key);
- d.errback(new goog.db.Error(
- (/** @type {IDBRequest} */ (ev.target)).errorCode,
- msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
return d;
};
@@ -196,7 +192,7 @@ goog.db.ObjectStore.prototype.get = function(key) {
} catch (err) {
var msg = 'getting from ' + this.getName() + ' with key ' +
goog.debug.deepExpose(key);
- d.errback(goog.db.Error.create(err, msg));
+ d.errback(goog.db.Error.fromException(err, msg));
return d;
}
request.onsuccess = function(ev) {
@@ -206,9 +202,7 @@ goog.db.ObjectStore.prototype.get = function(key) {
request.onerror = function(ev) {
var msg = 'getting from ' + self.getName() + ' with key ' +
goog.debug.deepExpose(key);
- d.errback(new goog.db.Error(
- (/** @type {IDBRequest} */ (ev.target)).errorCode,
- msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
return d;
};
@@ -297,9 +291,9 @@ goog.db.ObjectStore.prototype.openCursor = function(opt_range, opt_direction) {
} else {
request = this.store_.openCursor(range);
}
- } catch (err) {
+ } catch (ex) {
cursor.dispose();
- throw goog.db.Error.create(err, msg);
+ throw goog.db.Error.fromException(ex, msg);
}
request.onsuccess = function(ev) {
cursor.cursor_ = ev.target.result || null;
@@ -328,16 +322,14 @@ goog.db.ObjectStore.prototype.clear = function() {
try {
request = this.store_.clear();
} catch (err) {
- d.errback(goog.db.Error.create(err, msg));
+ d.errback(goog.db.Error.fromException(err, msg));
return d;
}
request.onsuccess = function(ev) {
d.callback();
};
request.onerror = function(ev) {
- d.errback(new goog.db.Error(
- (/** @type {IDBRequest} */ (ev.target)).errorCode,
- msg));
+ d.errback(goog.db.Error.fromRequest(ev.target, msg));
};
return d;
};
@@ -361,9 +353,9 @@ goog.db.ObjectStore.prototype.createIndex = function(
try {
return new goog.db.Index(this.store_.createIndex(
name, keyPath, opt_parameters));
- } catch (err) {
+ } catch (ex) {
var msg = 'creating new index ' + name + ' with key path ' + keyPath;
- throw goog.db.Error.create(err, msg);
+ throw goog.db.Error.fromException(ex, msg);
}
};
@@ -378,9 +370,9 @@ goog.db.ObjectStore.prototype.createIndex = function(
goog.db.ObjectStore.prototype.getIndex = function(name) {
try {
return new goog.db.Index(this.store_.index(name));
- } catch (err) {
+ } catch (ex) {
var msg = 'getting index ' + name;
- throw goog.db.Error.create(err, msg);
+ throw goog.db.Error.fromException(ex, msg);
}
};
@@ -395,8 +387,8 @@ goog.db.ObjectStore.prototype.getIndex = function(name) {
goog.db.ObjectStore.prototype.deleteIndex = function(name) {
try {
this.store_.deleteIndex(name);
- } catch (err) {
+ } catch (ex) {
var msg = 'deleting index ' + name;
- throw goog.db.Error.create(err, msg);
+ throw goog.db.Error.fromException(ex, msg);
}
};
View
9 closure/goog/db/transaction.js
@@ -107,9 +107,8 @@ goog.db.Transaction.prototype.dispatchError_ = function(ev) {
} else {
this.dispatchEvent({
type: goog.db.Transaction.EventTypes.ERROR,
- target: new goog.db.Error(
- (/** @type {IDBRequest} */ (ev.target)).errorCode,
- 'in transaction')
+ target: goog.db.Error.fromRequest(
+ /** @type {!IDBRequest} */ (ev.target), 'in transaction')
});
}
};
@@ -158,8 +157,8 @@ goog.db.Transaction.prototype.getDatabase = function() {
goog.db.Transaction.prototype.objectStore = function(name) {
try {
return new goog.db.ObjectStore(this.tx_.objectStore(name));
- } catch (err) {
- throw new goog.db.Error(err.code, 'getting object store ' + name);
+ } catch (ex) {
+ throw goog.db.Error.fromException(ex, 'getting object store ' + name);
}
};
View
2  closure/goog/deps.js
@@ -73,7 +73,7 @@ goog.addDependency('date/relative.js', ['goog.date.relative'], ['goog.i18n.DateT
goog.addDependency('date/utcdatetime.js', ['goog.date.UtcDateTime'], ['goog.date', 'goog.date.Date', 'goog.date.DateTime', 'goog.date.Interval']);
goog.addDependency('db/cursor.js', ['goog.db.Cursor'], ['goog.async.Deferred', 'goog.db.Error', 'goog.debug', 'goog.events.EventTarget']);
goog.addDependency('db/db.js', ['goog.db'], ['goog.async.Deferred', 'goog.db.Error', 'goog.db.IndexedDb', 'goog.db.Transaction']);
-goog.addDependency('db/error.js', ['goog.db.Error', 'goog.db.Error.ErrorCode', 'goog.db.Error.VersionChangeBlockedError'], ['goog.debug.Error']);
+goog.addDependency('db/error.js', ['goog.db.Error', 'goog.db.Error.ErrorCode', 'goog.db.Error.ErrorName', 'goog.db.Error.VersionChangeBlockedError'], ['goog.debug.Error']);
goog.addDependency('db/index.js', ['goog.db.Index'], ['goog.async.Deferred', 'goog.db.Error', 'goog.debug']);
goog.addDependency('db/indexeddb.js', ['goog.db.IndexedDb'], ['goog.async.Deferred', 'goog.db.Error', 'goog.db.Error.VersionChangeBlockedError', 'goog.db.ObjectStore', 'goog.db.Transaction', 'goog.db.Transaction.TransactionMode', 'goog.events.Event', 'goog.events.EventHandler', 'goog.events.EventTarget']);
goog.addDependency('db/keyrange.js', ['goog.db.KeyRange'], []);
Please sign in to comment.
Something went wrong with that request. Please try again.