Skip to content

Commit

Permalink
Improve error wrapping of IndexedDB errors (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem authored Feb 28, 2020
1 parent c5076f5 commit e238d37
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/adapters/IDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,25 @@ function createListRequest(cid, store, filters, done) {
return request;
}

class IDBError extends Error {
constructor(method, err) {
super(`IndexedDB ${method}() ${err.message}`);
this.name = err.name;
this.stack = err.stack;
}
}

/**
* IndexedDB adapter.
*
* This adapter doesn't support any options.
*/
export default class IDB extends BaseAdapter {
/* Expose the IDBError class publicly */
static get IDBError() {
return IDBError;
}

/**
* Constructor.
*
Expand All @@ -264,9 +277,7 @@ export default class IDB extends BaseAdapter {
}

_handleError(method, err) {
const error = new Error(`IndexedDB ${method}() ${err.message}`);
error.stack = err.stack;
throw error;
throw new IDBError(method, err);
}

/**
Expand Down
11 changes: 8 additions & 3 deletions test/adapters/IDB_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ describe("adapter.IDB", () => {

it("should prefix error encountered", () => {
sandbox.stub(db, "open").returns(Promise.reject("error"));
return db.list().should.be.rejectedWith(Error, /^IndexedDB list()/);
return db
.list()
.should.be.rejectedWith(IDB.IDBError, /^IndexedDB list()/);
});

it("should reject on transaction error", () => {
Expand All @@ -332,7 +334,10 @@ describe("adapter.IDB", () => {
});
return db
.list()
.should.be.rejectedWith(Error, "IndexedDB list() transaction error");
.should.be.rejectedWith(
IDB.IDBError,
"IndexedDB list() transaction error"
);
});

it("should isolate records by collection", async () => {
Expand Down Expand Up @@ -447,7 +452,7 @@ describe("adapter.IDB", () => {
});
return db
.importBulk([{ foo: "bar" }])
.should.be.rejectedWith(Error, /^IndexedDB importBulk()/);
.should.be.rejectedWith(IDB.IDBError, /^IndexedDB importBulk()/);
});
});

Expand Down

0 comments on commit e238d37

Please sign in to comment.