Skip to content

Commit d572c53

Browse files
committed
Add error code
1 parent 96d8d40 commit d572c53

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

idb-cache.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const DATA_TYPE = {
1919
const isIOS = /iP(hone|(o|a)d);/.test(window.navigator.userAgent);
2020

2121
export default class IDBCache {
22+
public static ERROR = {
23+
INVALID_ARGUMENT : 1,
24+
CANNOT_OPEN : 2,
25+
REQUEST_FAILED : 3,
26+
GET_EMPTY : 4,
27+
}
2228
private _indexedDB : IDBFactory;
2329
private _dbName : string;
2430
private _maxSize : number = 52428800; // 50MB
@@ -57,7 +63,7 @@ export default class IDBCache {
5763
return new Promise((resolve:Function, reject:Function) => {
5864
this._serializeData(value, (data, meta) => {
5965
if(meta.size === 0){
60-
reject();
66+
reject(IDBCache.ERROR.INVALID_ARGUMENT);
6167
return;
6268
}
6369
this._open((db) => {
@@ -89,14 +95,14 @@ export default class IDBCache {
8995
transaction.oncomplete = null;
9096
transaction.onerror = null;
9197
transaction.onabort = null;
92-
reject();
98+
reject(IDBCache.ERROR.REQUEST_FAILED);
9399
};
94100

95101
transaction.onabort = () => {
96102
transaction.oncomplete = null;
97103
transaction.onerror = null;
98104
transaction.onabort = null;
99-
reject();
105+
reject(IDBCache.ERROR.REQUEST_FAILED);
100106
}
101107

102108
try{
@@ -108,7 +114,7 @@ export default class IDBCache {
108114
}
109115
}, () => {
110116
// Open error
111-
reject();
117+
reject(IDBCache.ERROR.CANNOT_OPEN);
112118
});
113119
})
114120
});
@@ -134,19 +140,20 @@ export default class IDBCache {
134140
resolve(data);
135141
});
136142
}else{
137-
reject();
143+
// Can not find or expired
144+
reject(IDBCache.ERROR.GET_EMPTY);
138145
}
139146
};
140147

141148
request.onerror = () => {
142149
request.onsuccess = null;
143150
request.onerror = null;
144-
reject();
151+
reject(IDBCache.ERROR.REQUEST_FAILED);
145152
};
146153
},
147154
() => {
148155
// Open error
149-
reject();
156+
reject(IDBCache.ERROR.CANNOT_OPEN);
150157
});
151158
});
152159
}
@@ -178,14 +185,14 @@ export default class IDBCache {
178185
transaction.oncomplete = null;
179186
transaction.onerror = null;
180187
transaction.onabort = null;
181-
reject();
188+
reject(IDBCache.ERROR.REQUEST_FAILED);
182189
};
183190

184191
transaction.onabort = () => {
185192
transaction.oncomplete = null;
186193
transaction.onerror = null;
187194
transaction.onabort = null;
188-
reject();
195+
reject(IDBCache.ERROR.REQUEST_FAILED);
189196
}
190197

191198
try{
@@ -198,7 +205,7 @@ export default class IDBCache {
198205
},
199206
() => {
200207
// Open error
201-
reject();
208+
reject(IDBCache.ERROR.CANNOT_OPEN);
202209
});
203210
});
204211
}

test/index.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ describe('Basic', function() {
1313
it('Blob', function() {
1414
return idbc.set('baz', new Blob([new ArrayBuffer(128)]));
1515
});
16+
it('empty string is ERROR.INVALID_ARGUMENT', function() {
17+
return idbc.set('empty', '').then(
18+
() => assert.fail(),
19+
(errorCode) => assert.deepEqual(IDBCache.ERROR.INVALID_ARGUMENT, errorCode)
20+
);
21+
});
22+
it('empty ArrayBuffer is ERROR.INVALID_ARGUMENT', function() {
23+
return idbc.set('empty', new ArrayBuffer()).then(
24+
() => assert.fail(),
25+
(errorCode) => assert.deepEqual(IDBCache.ERROR.INVALID_ARGUMENT, errorCode)
26+
);
27+
});
28+
it('empty Blob is ERROR.INVALID_ARGUMENT', function() {
29+
return idbc.set('empty', new Blob([new ArrayBuffer()])).then(
30+
() => assert.fail(),
31+
(errorCode) => assert.deepEqual(IDBCache.ERROR.INVALID_ARGUMENT, errorCode)
32+
);
33+
});
1634
});
1735
describe('#update', function() {
1836
it('string', function() {
@@ -43,6 +61,12 @@ describe('Basic', function() {
4361
assert.strictEqual(data.size, 256);
4462
});
4563
});
64+
it('Can not find is ERROR.GET_EMPTY', function() {
65+
return idbc.get('empty').then(
66+
() => assert.fail(),
67+
(errorCode) => assert.deepEqual(IDBCache.ERROR.GET_EMPTY, errorCode)
68+
);
69+
});
4670
});
4771
describe('#delete', function() {
4872
it('string', function() {
@@ -175,10 +199,13 @@ describe('Limit Management', function() {
175199
() => idbc.get('foo')
176200
).then(
177201
() => assert.fail(), // foo should have been deleted
178-
() => idbc.get('bar')
202+
(errorCode) => {
203+
assert.deepEqual(IDBCache.ERROR.GET_EMPTY, errorCode)
204+
return idbc.get('bar');
205+
}
179206
).then(
180207
() => assert.fail(), // bar should have been deleted
181-
() => assert.ok(true)
208+
(errorCode) => assert.deepEqual(IDBCache.ERROR.GET_EMPTY, errorCode)
182209
);
183210
});
184211

@@ -198,7 +225,7 @@ describe('Limit Management', function() {
198225
() => idbc.get('bar')
199226
).then(
200227
() => assert.fail(), // bar should have been deleted
201-
() => assert.ok(true)
228+
(errorCode) => assert.deepEqual(IDBCache.ERROR.GET_EMPTY, errorCode)
202229
);
203230
});
204231
});

0 commit comments

Comments
 (0)