Skip to content

Commit 0ad00f6

Browse files
committed
Incorporated more review feedback.
1 parent d91afff commit 0ad00f6

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

packages/datafile-manager/__test__/reactNativeAsyncStorageCache.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ describe('reactNativeAsyncStorageCache', () => {
3434
})
3535

3636
it('should reject promise error if string has an incorrect JSON format', function() {
37-
return cacheInstance.get('keyWithInvalidJsonObject').catch(() => {})
37+
return cacheInstance.get('keyWithInvalidJsonObject')
38+
.catch(() => 'exception caught').then(v => { expect(v).toEqual('exception caught') })
3839
})
3940
})
4041

@@ -47,7 +48,8 @@ describe('reactNativeAsyncStorageCache', () => {
4748
it('should reject promise if item was not set in the cache because of json stringifying error', function() {
4849
const testObj: any = { name: "Awesome Object" }
4950
testObj.myOwnReference = testObj
50-
return cacheInstance.set('testKey', testObj).catch(() => {})
51+
return cacheInstance.set('testKey', testObj)
52+
.catch(() => 'exception caught').then(v => expect(v).toEqual('exception caught'))
5153
})
5254
})
5355

packages/datafile-manager/src/reactNativeAsyncStorageCache.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,27 @@ import PersistentKeyValueCache from './persistentKeyValueCache'
2222
const logger = getLogger('DatafileManager')
2323

2424
export default class ReactNativeAsyncStorageCache implements PersistentKeyValueCache {
25-
2625
get(key: string): Promise<any | null> {
27-
try {
28-
return AsyncStorage.getItem(key)
29-
.then((val: string | null) => val ? JSON.parse(val) : null)
30-
} catch (ex) {
31-
logger.error('Error Parsing Object from cache - %s', ex)
32-
return Promise.resolve(ex);
33-
}
26+
return AsyncStorage.getItem(key)
27+
.then((val: string | null) => {
28+
if (!val) {
29+
return null
30+
}
31+
try {
32+
return JSON.parse(val);
33+
} catch (ex) {
34+
logger.error('Error Parsing Object from cache - %s', ex)
35+
throw ex
36+
}
37+
})
3438
}
3539

3640
set(key: string, val: any): Promise<void> {
3741
try {
3842
return AsyncStorage.setItem(key, JSON.stringify(val))
39-
} catch (e) {
40-
logger.error('Error stringifying Object to Json - %s', e)
41-
return Promise.reject(e)
43+
} catch (ex) {
44+
logger.error('Error stringifying Object to Json - %s', ex)
45+
return Promise.reject(ex)
4246
}
4347
}
4448

0 commit comments

Comments
 (0)