Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BulkError: Add failuresByPos property #1209

Merged
merged 2 commits into from Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions test/tests-table.js
Expand Up @@ -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");
}
});