diff --git a/src/classes/table/table.ts b/src/classes/table/table.ts index ff1c9d753..7d9a7a913 100644 --- a/src/classes/table/table.ts +++ b/src/classes/table/table.ts @@ -430,8 +430,7 @@ export class Table implements ITable { const result = wantResults ? results : lastResult; if (numFailures === 0) return result; throw new BulkError( - `${this.name}.bulkAdd(): ${numFailures} of ${numObjects} operations failed`, - Object.keys(failures).map(pos => failures[pos])); + `${this.name}.bulkAdd(): ${numFailures} of ${numObjects} operations failed`, failures); }); }); } @@ -469,8 +468,7 @@ export class Table implements ITable { const result = wantResults ? results : lastResult; if (numFailures === 0) return result; throw new BulkError( - `${this.name}.bulkPut(): ${numFailures} of ${numObjects} operations failed`, - Object.keys(failures).map(pos => failures[pos])); + `${this.name}.bulkPut(): ${numFailures} of ${numObjects} operations failed`, failures); }); }); } diff --git a/src/errors/errors.js b/src/errors/errors.js index cbc124fc6..0e5ecc34d 100644 --- a/src/errors/errors.js +++ b/src/errors/errors.js @@ -94,7 +94,8 @@ derive(ModifyError).from(DexieError); export function BulkError (msg, failures) { this._e = getErrorWithStack(); this.name = "BulkError"; - this.failures = failures; + this.failures = Object.keys(failures).map(pos => failures[pos]); + this.failuresByPos = failures; this.message = getMultiErrorMessage(msg, failures); } derive(BulkError).from(DexieError); diff --git a/src/public/types/errors.d.ts b/src/public/types/errors.d.ts index 279dbe993..7cb63216c 100644 --- a/src/public/types/errors.d.ts +++ b/src/public/types/errors.d.ts @@ -137,7 +137,8 @@ export interface ModifyError extends DexieError { * http://dexie.org/docs/DexieErrors/Dexie.BulkError */ export interface BulkError extends DexieError { - failures: {[operationNumber: number]: Error}; + failures: Error[]; + failuresByPos: {[operationNumber: number]: Error}; } export interface DexieErrorConstructor { diff --git a/test/tests-table.js b/test/tests-table.js index b8fa9ffe2..375e00fb7 100644 --- a/test/tests-table.js +++ b/test/tests-table.js @@ -935,3 +935,23 @@ promisedTest("bulkGet()", async () => { ok(u3 && u3.first === 'Foo100', "Third should be Foo100"); ok(u4 === undefined, "Forth should be undefined"); }); + +promisedTest("bulkError by pos", async () => { + try { + const ids = await db.users.bulkAdd([ + { first: "foo1", last: "bar1", username: "foobar" }, + { first: "foo2", last: "bar2", username: "foobar" }, // should fail because username is unique idx + { first: "foo3", last: "bar3", username: "foobar3" }, + ]); + ok(false, "Should not succeed"); + } catch (bulkError) { + ok(bulkError instanceof Dexie.BulkError, "Got BulkError"); + equal(bulkError.failures.length, 1, "Got one failure"); + ok(bulkError.failures[0] instanceof Error, "failures[0] instanceof Error"); + ok(bulkError.failures[1] === undefined, "failures[1] is undefined"); + equal(Object.keys(bulkError.failuresByPos).length, 1, "Got one key in failuresByPos"); + equal(Object.keys(bulkError.failuresByPos)[0], 1, "Failure in position 1"); + ok(bulkError.failuresByPos[0] === undefined, "failuresByPos[0] is undefined"); + ok(bulkError.failuresByPos[1] instanceof Error, "failuresByPos[1] instanceof Error"); + } +});