0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00

Added error handling for cache reset function

Makes sure the Ghost process does not blow up in case there's an error in
communication with Redis
This commit is contained in:
Naz 2023-05-09 15:48:51 +07:00 committed by Fabien 'egg' O'Carroll
parent b84ed78078
commit 05a4d2cc79
2 changed files with 32 additions and 3 deletions

View file

@ -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);
}
}

View file

@ -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');
});
});
});