fix: add error messages on cache
This commit is contained in:
@@ -1,10 +1,14 @@
|
|||||||
import {default as Keyv, Store} from 'keyv';
|
import {default as Keyv, Store} from 'keyv';
|
||||||
import env from './config/config';
|
import env from './config/config';
|
||||||
import {default as KeyvRedis} from '@keyv/redis';
|
import {default as KeyvRedis} from '@keyv/redis';
|
||||||
|
|
||||||
// check if redis is defined
|
// check if redis is defined
|
||||||
|
|
||||||
const cache: Keyv | null = getCache();
|
const cache: Keyv | null = getCache();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getCache(): Keyv | null {
|
function getCache(): Keyv | null {
|
||||||
if(env['CACHE_ENABLED'] !== true) return null;
|
if(env['CACHE_ENABLED'] !== true) return null;
|
||||||
|
|
||||||
@@ -25,18 +29,31 @@ function getCache(): Keyv | null {
|
|||||||
uri = env['REDIS']
|
uri = env['REDIS']
|
||||||
|
|
||||||
if(uri == null || uri === '') {
|
if(uri == null || uri === '') {
|
||||||
uri = `redis://${env['REDIS_USERNAME']}:${env['REDIS_PASSWORD']}@${env['REDIS_HOST']}:${env['REDIS_PORT']}`;
|
uri = `redis://${env['REDIS_USERNAME'] }:${env['REDIS_PASSWORD'] || ''}@${env['REDIS_HOST']}:${env['REDIS_PORT']}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
store = new KeyvRedis(uri);
|
|
||||||
|
|
||||||
|
store = new KeyvRedis(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Keyv(uri, {
|
try {
|
||||||
namespace: namespace,
|
const keyv = new Keyv(uri, {
|
||||||
ttl,
|
namespace: namespace,
|
||||||
store: store
|
ttl,
|
||||||
});
|
store: store
|
||||||
|
});
|
||||||
|
|
||||||
|
keyv.on('error', (err) => {
|
||||||
|
throw new Error("CACHE: could not connect: " + err)
|
||||||
|
});
|
||||||
|
|
||||||
|
return keyv
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error("CACHE: could not connect to database: " + e)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CacheEnabled(): boolean {
|
export function CacheEnabled(): boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user