diff --git a/api/src/exceptions/index.ts b/api/src/exceptions/index.ts index d98537e18e..b539ff49ff 100644 --- a/api/src/exceptions/index.ts +++ b/api/src/exceptions/index.ts @@ -8,4 +8,5 @@ export * from './invalid-payload'; export * from './invalid-query'; export * from './item-limit'; export * from './item-not-found'; +export * from './redis-not-found'; export * from './route-not-found'; diff --git a/api/src/exceptions/redis-not-found.ts b/api/src/exceptions/redis-not-found.ts new file mode 100644 index 0000000000..4eafc7866f --- /dev/null +++ b/api/src/exceptions/redis-not-found.ts @@ -0,0 +1,7 @@ +import { BaseException } from './base'; + +export class RedisNotFoundException extends BaseException { + constructor(message: string) { + super(message, 404, 'REDIS_NOT_FOUND'); + } +} diff --git a/api/src/middleware/rate-limiter.ts b/api/src/middleware/rate-limiter.ts index e82c280103..93d3d95a94 100644 --- a/api/src/middleware/rate-limiter.ts +++ b/api/src/middleware/rate-limiter.ts @@ -8,6 +8,7 @@ import { RequestHandler } from 'express'; import redis from 'redis'; import { RateLimiterRedis } from 'rate-limiter-flexible'; import { HitRateLimitException } from '../exceptions'; +import { RedisNotFoundException } from '../exceptions'; import env from '../env'; const redisClient = redis.createClient({ @@ -21,8 +22,7 @@ const rateLimiter: RequestHandler = (req, res, next) => { try { // first need to check that redis is running! if (!redisClient) { - throw new Error('Redis client does not exist'); - process.exit(1); + throw new RedisNotFoundException('Redis client does not exist'); } // options for the rate limiter are set below. Opts can be found // at https://github.com/animir/node-rate-limiter-flexible/wiki/Options @@ -46,8 +46,8 @@ const rateLimiter: RequestHandler = (req, res, next) => { next(); }) .catch((rejRes) => { - if (rejRes instanceof Error) { - throw new Error('Redis insurance limiter not set up'); + if (rejRes instanceof RedisNotFoundException) { + throw new RedisNotFoundException('Redis insurance limiter not set up'); } else { // If there is no error, rateLimiterRedis promise rejected with number of ms before next request allowed const secs = Math.round(rejRes.msBeforeNext / 1000) || 1;