diff --git a/src/index.ts b/src/index.ts index 8467fbe..c8fe0b6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,8 +4,9 @@ export function promisifyRequest( return new Promise((resolve, reject) => { // @ts-ignore - file size hacks request.oncomplete = request.onsuccess = () => resolve(request.result); + request.onerror = (e) => reject((e.target as IDBRequest | IDBTransaction).error); // @ts-ignore - file size hacks - request.onabort = request.onerror = () => reject(request.error); + request.onabort = () => reject(request.error); }); } diff --git a/test/index.ts b/test/index.ts index 19540b7..e0e321c 100644 --- a/test/index.ts +++ b/test/index.ts @@ -13,7 +13,8 @@ import { setMany, update, getMany, - delMany + delMany, + UseStore } from '../src'; import { assert as typeAssert, IsExact } from 'conditional-type-checks'; @@ -23,6 +24,11 @@ mocha.setup('tdd'); (async () => { await promisifyRequest(indexedDB.deleteDatabase('keyval-store')); const customStore = createStore('custom-db', 'custom-kv'); + const errorStore: UseStore = (txMode, callback) => customStore(txMode, (store) => { + const result = callback(store); + store.transaction.abort(); + return result; + }) suite('The basics', () => { test('get & set', async () => { @@ -152,6 +158,28 @@ mocha.setup('tdd'); 'Error is correct type', ); } + + try { + await get('foo', errorStore); + assert.fail('Expected throw'); + } catch (err) { + assert.strictEqual( + (err as DOMException).name, + 'AbortError', + 'Error is correct type', + ); + } + + try { + await set('foo', 'bar', errorStore); + assert.fail('Expected throw'); + } catch (err) { + assert.strictEqual( + (err as DOMException).name, + 'AbortError', + 'Error is correct type', + ); + } }); });