Skip to content

Commit

Permalink
change missing req.ip from thrown error to logged error.
Browse files Browse the repository at this point in the history
relates to #254, #260
  • Loading branch information
nfriedly committed Nov 6, 2021
1 parent 03bb47c commit 5fb38fa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/express-rate-limit.js
Expand Up @@ -19,8 +19,8 @@ function RateLimit(options) {
// allows to create custom keys (by default user IP is used)
keyGenerator: function (req /*, res*/) {
if (!req.ip) {
throw new Error(
"express-rate-limit: req.ip is undefined - are you sure you're using express?"
console.error(
"express-rate-limit: req.ip is undefined - you can avoid this by providing a custom keyGenerator function, but it may be indicative of a larger issue."
);
}
return req.ip;
Expand Down
10 changes: 5 additions & 5 deletions test/express-rate-limit-test.js
Expand Up @@ -3,6 +3,7 @@ const express = require("express");
const assert = require("assert");
const request = require("supertest");
const sinon = require("sinon");
const sandbox = sinon.createSandbox();
const rateLimit = require("../lib/express-rate-limit.js");
const { promisify } = require("util");

Expand All @@ -17,6 +18,7 @@ describe("express-rate-limit node module", () => {

afterEach(() => {
clock.restore();
sandbox.restore();
});

function createAppWith(limit) {
Expand Down Expand Up @@ -89,11 +91,9 @@ describe("express-rate-limit node module", () => {
it("should error when req.ip is undefined", async () => {
const limiter = rateLimit({});
const { IncomingMessage, OutgoingMessage } = require("http");
await assert.rejects(
promisify(limiter)(new IncomingMessage(), new OutgoingMessage()),
/express/,
"Should error with a message about express"
);
sandbox.stub(console, "error");
await promisify(limiter)(new IncomingMessage(), new OutgoingMessage());
sandbox.assert.calledOnce(console.error);
});

it("should let the first request through", async () => {
Expand Down

0 comments on commit 5fb38fa

Please sign in to comment.