preserve value for storage errors on add and put

This commit is contained in:
Bruno Windels 2019-10-12 22:19:16 +02:00
parent 201b70ee4f
commit b16f21867c
2 changed files with 17 additions and 5 deletions

View File

@ -13,7 +13,7 @@ export const STORE_MAP = Object.freeze(STORE_NAMES.reduce((nameMap, name) => {
}, {}));
export class StorageError extends Error {
constructor(message, cause) {
constructor(message, cause, value) {
let fullMessage = message;
if (cause) {
fullMessage += ": ";
@ -26,5 +26,7 @@ export class StorageError extends Error {
if (cause) {
this.errcode = cause.name;
}
this.cause = cause;
this.value = value;
}
}

View File

@ -93,12 +93,22 @@ export default class Store extends QueryTarget {
return new QueryTarget(new QueryTargetWrapper(this._idbStore.index(indexName)));
}
put(value) {
return reqAsPromise(this._idbStore.put(value));
async put(value) {
try {
return await reqAsPromise(this._idbStore.put(value));
} catch(err) {
const originalErr = err.cause;
throw new StorageError(`put on ${this._idbStore.name} failed`, originalErr, value);
}
}
add(value) {
return reqAsPromise(this._idbStore.add(value));
async add(value) {
try {
return await reqAsPromise(this._idbStore.add(value));
} catch(err) {
const originalErr = err.cause;
throw new StorageError(`add on ${this._idbStore.name} failed`, originalErr, value);
}
}
delete(keyOrKeyRange) {