mirror of
https://github.com/directus/directus.git
synced 2026-01-29 11:48:15 -05:00
Some error catching for the node cache
This commit is contained in:
@@ -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';
|
||||
|
||||
7
api/src/exceptions/invalid-cache-key.ts
Normal file
7
api/src/exceptions/invalid-cache-key.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { BaseException } from './base';
|
||||
|
||||
export class InvalidCacheKeyException extends BaseException {
|
||||
constructor(message: string) {
|
||||
super(message, 400, 'INVALID_CACHE_KEY');
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user