Some error catching for the node cache

This commit is contained in:
Tanya Byrne
2020-08-21 15:54:31 +01:00
parent a040b021ce
commit 0e54ecd9ec
3 changed files with 15 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,7 @@
import { BaseException } from './base';
export class InvalidCacheKeyException extends BaseException {
constructor(message: string) {
super(message, 400, 'INVALID_CACHE_KEY');
}
}

View File

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