From 0e54ecd9ecebd9386c75706ae72c39c8dc1f06a0 Mon Sep 17 00:00:00 2001 From: Tanya Byrne Date: Fri, 21 Aug 2020 15:54:31 +0100 Subject: [PATCH] Some error catching for the node cache --- api/src/exceptions/index.ts | 1 + api/src/exceptions/invalid-cache-key.ts | 7 +++++++ api/src/services/node-cache.ts | 7 +++++++ 3 files changed, 15 insertions(+) create mode 100644 api/src/exceptions/invalid-cache-key.ts diff --git a/api/src/exceptions/index.ts b/api/src/exceptions/index.ts index b539ff49ff..d15357556e 100644 --- a/api/src/exceptions/index.ts +++ b/api/src/exceptions/index.ts @@ -3,6 +3,7 @@ export * from './collection-not-found'; export * from './field-not-found'; export * from './forbidden'; export * from './hit-rate-limit'; +export * from './invalid-cache-key'; export * from './invalid-credentials'; export * from './invalid-payload'; export * from './invalid-query'; diff --git a/api/src/exceptions/invalid-cache-key.ts b/api/src/exceptions/invalid-cache-key.ts new file mode 100644 index 0000000000..b61510ddf7 --- /dev/null +++ b/api/src/exceptions/invalid-cache-key.ts @@ -0,0 +1,7 @@ +import { BaseException } from './base'; + +export class InvalidCacheKeyException extends BaseException { + constructor(message: string) { + super(message, 400, 'INVALID_CACHE_KEY'); + } +} diff --git a/api/src/services/node-cache.ts b/api/src/services/node-cache.ts index 8cf1d33fd9..639f93d9dd 100644 --- a/api/src/services/node-cache.ts +++ b/api/src/services/node-cache.ts @@ -9,6 +9,7 @@ * Have wrapped node cache so we can extend if needed */ import NodeCache from 'node-cache'; +import { InvalidCacheKeyException } from '../exceptions'; export default class NodeCacheService { apiCache: NodeCache; @@ -25,6 +26,9 @@ export default class NodeCacheService { // so might as well do it here too for consitancy async delCache(keys: string) { + if (!keys) { + throw new InvalidCacheKeyException('Keys was not provided for cache'); + } this.apiCache.del(keys); } // attempt to get the cache based on the key, if it is empty then set it @@ -32,6 +36,9 @@ export default class NodeCacheService { // convert string to json async getCache(key: string, setData: string) { + if (!setData) { + throw new InvalidCacheKeyException('No response data was provided for cache'); + } // first get the value const value = this.apiCache.get(key);