Skip to content

Commit

Permalink
Extended BulkError with failuresByPos.
Browse files Browse the repository at this point in the history
* Correction of typings of BulkError.failures - they are a plain array of Error objects.
* Added failuresByPos which contains a map object between the position number of the operation to the actual error.

See #753
  • Loading branch information
dfahlander committed Jan 12, 2021
1 parent 793ab91 commit 3ddfd98
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
6 changes: 2 additions & 4 deletions src/classes/table/table.ts
Expand Up @@ -430,8 +430,7 @@ export class Table implements ITable<any, IndexableType> {
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);
});
});
}
Expand Down Expand Up @@ -469,8 +468,7 @@ export class Table implements ITable<any, IndexableType> {
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);
});
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/errors/errors.js
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/public/types/errors.d.ts
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions test/tests-table.js
Expand Up @@ -935,3 +935,18 @@ 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) {
equal(bulkError.failures.length === 1, "Got one failure");
equal(Object.keys(bulkError.failuresByPos).length, 1, "Got one key in failuresByPos");
equal(Object.keys(bulkError.failuresByPos)[0], 1, "Failure in position 1");
}
});

0 comments on commit 3ddfd98

Please sign in to comment.