Skip to content

Commit

Permalink
Handle exceptions
Browse files Browse the repository at this point in the history
Allow express to handle exceptions with default error handler. Now, we
can throw an exception into `handler` callback if limits reached.

@see https://expressjs.com/en/guide/error-handling.html#the-default-error-handler
  • Loading branch information
wandersonwhcr committed Apr 28, 2020
1 parent 05dbbd3 commit e490c8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/express-rate-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ function RateLimit(options) {
}

next();
});
})
.catch(next);
});
}

Expand Down
24 changes: 24 additions & 0 deletions test/express-rate-limit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,28 @@ describe("express-rate-limit node module", () => {
rateLimit(opts);
assert.deepEqual(opts, {});
});

it("should handle exceptions", (done) => {
const store = new MockStore();
const app = createAppWith(
rateLimit({
max: 1,
store: store,
handler: () => {
const exception = new Error();
exception.code = 429;
exception.message = "Too many requests";
throw exception;
},
})
);
app.use((err, req, res, next) => {
res.status(err.code).send(err.message);
next;
});
goodRequest(done);
badRequest(done, () => {
done();
});
});
});

0 comments on commit e490c8b

Please sign in to comment.