From c23d668e18e3a11cbee25a8d13c81410b9271236 Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Wed, 30 Aug 2023 13:52:18 +0000 Subject: [PATCH] fix: add error messages on cache --- src/external-jwt/cache.ts | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/external-jwt/cache.ts b/src/external-jwt/cache.ts index 3718d25..3baebd8 100644 --- a/src/external-jwt/cache.ts +++ b/src/external-jwt/cache.ts @@ -1,10 +1,14 @@ import {default as Keyv, Store} from 'keyv'; import env from './config/config'; import {default as KeyvRedis} from '@keyv/redis'; + // check if redis is defined const cache: Keyv | null = getCache(); + + + function getCache(): Keyv | null { if(env['CACHE_ENABLED'] !== true) return null; @@ -25,18 +29,31 @@ function getCache(): Keyv | null { uri = env['REDIS'] 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); - } - return new Keyv(uri, { - namespace: namespace, - ttl, - store: store - }); + try { + const keyv = new Keyv(uri, { + namespace: namespace, + 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 {