Skip to content

Commit 2a30332

Browse files
author
Simone Sanfratello
authored
fix: error handling in references function (#34)
* fix: error handling in references function * fix: onError event in references function error
1 parent be95f7a commit 2a30332

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929
"license": "MIT",
3030
"devDependencies": {
3131
"@fastify/pre-commit": "^2.0.2",
32-
"ioredis": "^5.0.1",
32+
"ioredis": "^5.2.3",
3333
"proxyquire": "^2.1.3",
3434
"snazzy": "^9.0.0",
3535
"standard": "^17.0.0",
36-
"tap": "^16.2.0"
36+
"tap": "^16.3.0"
3737
},
3838
"dependencies": {
3939
"abstract-logging": "^2.0.1",
40-
"mnemonist": "^0.39.0",
41-
"safe-stable-stringify": "^2.2.0"
40+
"mnemonist": "^0.39.2",
41+
"safe-stable-stringify": "^2.3.1"
4242
},
4343
"standard": {
4444
"ignore": [

src/cache.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,14 @@ class Wrapper {
261261
return result
262262
}
263263

264-
let references = this.references(args, key, result)
265-
if (references && typeof references.then === 'function') { references = await references }
266-
// TODO validate references?
267-
await this.storage.set(storageKey, result, this.ttl, references)
264+
try {
265+
let references = this.references(args, key, result)
266+
if (references && typeof references.then === 'function') { references = await references }
267+
// TODO validate references?
268+
await this.storage.set(storageKey, result, this.ttl, references)
269+
} catch (err) {
270+
this.onError(err)
271+
}
268272

269273
return result
270274
}

test/base.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,34 @@ test('should cache with references', async function (t) {
491491
await cache.run(1)
492492
})
493493

494+
test('should handle references function (sync) throwing an error', async function (t) {
495+
t.plan(4)
496+
497+
const cache = new Cache({ ttl: 60, storage: createStorage() })
498+
499+
cache.define('references', {
500+
onError: (err) => { t.equal(err.message, 'boom') },
501+
references: (args, key, result) => { throw new Error('boom') }
502+
}, () => 'the-result')
503+
504+
t.equal(await cache.references(1), 'the-result')
505+
t.equal(await cache.references(1), 'the-result')
506+
})
507+
508+
test('should handle references function (async) throwing an error', async function (t) {
509+
t.plan(4)
510+
511+
const cache = new Cache({ ttl: 60, storage: createStorage() })
512+
513+
cache.define('references', {
514+
onError: (err) => { t.equal(err.message, 'boom') },
515+
references: async (args, key, result) => { throw new Error('boom') }
516+
}, () => 'the-result')
517+
518+
t.equal(await cache.references(1), 'the-result')
519+
t.equal(await cache.references(1), 'the-result')
520+
})
521+
494522
test('should cache with async references', async function (t) {
495523
t.plan(1)
496524

0 commit comments

Comments
 (0)