updating slightly - fixed a few bugs as was bypassing errors say if auth failed

This commit is contained in:
Tanya Byrne
2020-08-18 13:43:14 +01:00
parent 947a5ee557
commit 542b4447c5
2 changed files with 10 additions and 7 deletions

View File

@@ -41,12 +41,15 @@ const rateLimiter: RequestHandler = asyncHandler(async (req, res, next) => {
try {
await rateLimiterRedis.consume(req.ip);
} catch (err) {
// If there is no error, rateLimiterRedis promise rejected with number of ms before next request allowed
const secs = Math.round(err.msBeforeNext / 1000) || 1;
res.set('Retry-After', String(secs));
res.status(429).send('Too Many Requests');
throw new HitRateLimitException(`Too many requests, retry after ${secs}.`);
} catch (rejRes) {
if (rejRes instanceof Error) {
throw Error;
} 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;
res.set('Retry-After', String(secs));
throw new HitRateLimitException(`Too many requests, retry after ${secs}.`);
}
}
return next();