feat(cache): enhance error handling and logging for cache operations
- Added try-catch blocks to improve resilience in cache operations across services. - Integrated detailed error logging for better debugging. - Updated Redis reconnect strategy with exponential backoff and logging.
This commit is contained in:
@@ -49,13 +49,18 @@ export class CrawlerDetectionMiddleware implements NestMiddleware {
|
||||
const userAgent = req.get("user-agent") || "unknown";
|
||||
|
||||
// Vérifier si l'IP est bannie
|
||||
const isBanned = await this.cacheManager.get(`banned_ip:${ip}`);
|
||||
if (isBanned) {
|
||||
this.logger.warn(`Banned IP attempt: ${ip} -> ${method} ${url}`);
|
||||
res.status(403).json({
|
||||
message: "Access denied: Your IP has been temporarily banned.",
|
||||
});
|
||||
return;
|
||||
try {
|
||||
const isBanned = await this.cacheManager.get(`banned_ip:${ip}`);
|
||||
if (isBanned) {
|
||||
this.logger.warn(`Banned IP attempt: ${ip} -> ${method} ${url}`);
|
||||
res.status(403).json({
|
||||
message: "Access denied: Your IP has been temporarily banned.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(`Error checking ban status for IP ${ip}: ${error.message}`);
|
||||
// On continue même en cas d'erreur Redis pour ne pas bloquer les utilisateurs légitimes
|
||||
}
|
||||
|
||||
res.on("finish", async () => {
|
||||
@@ -73,7 +78,11 @@ export class CrawlerDetectionMiddleware implements NestMiddleware {
|
||||
);
|
||||
|
||||
// Bannir l'IP pour 24h via Redis
|
||||
await this.cacheManager.set(`banned_ip:${ip}`, true, 86400000);
|
||||
try {
|
||||
await this.cacheManager.set(`banned_ip:${ip}`, true, 86400000);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error banning IP ${ip}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user