diff --git a/ghost/adapter-cache-redis/lib/AdapterCacheRedis.js b/ghost/adapter-cache-redis/lib/AdapterCacheRedis.js index c6a1799d8e..f943fcdb2c 100644 --- a/ghost/adapter-cache-redis/lib/AdapterCacheRedis.js +++ b/ghost/adapter-cache-redis/lib/AdapterCacheRedis.js @@ -144,9 +144,13 @@ class AdapterCacheRedis extends BaseCacheAdapter { * Reset the cache by deleting everything from redis */ async reset() { - const keys = await this.#getKeys(); - for (const key of keys) { - await this.cache.del(key); + try { + const keys = await this.#getKeys(); + for (const key of keys) { + await this.cache.del(key); + } + } catch (err) { + logging.error(err); } } diff --git a/ghost/adapter-cache-redis/test/adapter-cache-redis.test.js b/ghost/adapter-cache-redis/test/adapter-cache-redis.test.js index 5de91a103c..4655c6e808 100644 --- a/ghost/adapter-cache-redis/test/adapter-cache-redis.test.js +++ b/ghost/adapter-cache-redis/test/adapter-cache-redis.test.js @@ -1,8 +1,13 @@ const assert = require('assert/strict'); const sinon = require('sinon'); +const logging = require('@tryghost/logging'); const RedisCache = require('../index'); describe('Adapter Cache Redis', function () { + beforeEach(function () { + sinon.stub(logging, 'error'); + }); + afterEach(function () { sinon.restore(); }); @@ -82,4 +87,24 @@ describe('Adapter Cache Redis', function () { assert.equal(redisCacheInstanceStub.set.args[0][0], 'testing-prefix:key-here'); }); }); + + describe('reset', function () { + it('catches an error when thrown during the reset', async function () { + const redisCacheInstanceStub = { + get: sinon.stub().resolves('value from cache'), + store: { + getClient: sinon.stub().returns({ + on: sinon.stub() + }) + } + }; + const cache = new RedisCache({ + cache: redisCacheInstanceStub + }); + + await cache.reset(); + + assert.ok(logging.error.calledOnce, 'error was logged'); + }); + }); });