Skip to content

Commit bada9f8

Browse files
committed
RateLimiter - Replace message responses with custom handler for better error handling
1 parent 7f16f36 commit bada9f8

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

src/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ const globalLimiter = rateLimit({
9494
max: env.GLOBAL_RATE_LIMIT_MAX,
9595
standardHeaders: true,
9696
legacyHeaders: false,
97-
skip: req => mute.has(req.path) || req.path.startsWith("/api-docs")
97+
skip: req => mute.has(req.path) || req.path.startsWith("/api-docs"),
98+
handler: (req, res, next) => next(tooManyRequests("Too many requests"))
9899
});
99100

100101
app.use(globalLimiter);

src/middleware/rateLimit.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import rateLimit from "express-rate-limit";
22
import {env} from "../config/env.js";
3+
import {tooManyRequests} from "../utils/httpError.js";
34

45
export const purchaseLimiter = rateLimit({
56
windowMs: env.PURCHASE_RATE_LIMIT_WINDOW_MS,
67
max: env.PURCHASE_RATE_LIMIT_MAX,
78
standardHeaders: true,
89
legacyHeaders: false,
9-
message: {error: {code: "TOO_MANY_REQUESTS", message: "Too many purchase requests"}}
10+
handler: (req, res, next) => next(tooManyRequests("Too many purchase requests"))
1011
});

0 commit comments

Comments
 (0)