Add retry in indication to TooManyRequests http error

This commit is contained in:
Alice Gaudon 2020-04-24 11:17:58 +02:00
parent 3397fd8216
commit 7db6c0e0c7
3 changed files with 7 additions and 7 deletions

View File

@ -1,6 +1,6 @@
{
"name": "wms-core",
"version": "0.2.10",
"version": "0.2.11",
"description": "Node web framework",
"repository": "git@gitlab.com:ArisuOngaku/wms-core.git",
"author": "Alice Gaudon <alice@gaudon.pro>",

View File

@ -59,10 +59,10 @@ export class NotFoundHttpError extends BadRequestError {
}
export class TooManyRequestsHttpError extends BadRequestError {
constructor(cause?: Error) {
constructor(retryIn: number, cause?: Error) {
super(
`You're making too many requests!`,
`We need some rest.`,
`We need some rest. Please retry in ${Math.floor(retryIn / 1000)} seconds.`,
'',
cause
);

View File

@ -54,7 +54,7 @@ class Throttle {
let currentDate = new Date().getTime();
if (trigger.jailed && currentDate - trigger.jailed < this.jailPeriod) {
this.throw();
this.throw((trigger.jailed + this.jailPeriod) - currentDate);
return;
}
@ -71,12 +71,12 @@ class Throttle {
if (trigger.count > this.max) {
trigger.jailed = currentDate;
this.throw();
this.throw((trigger.jailed + this.jailPeriod) - currentDate);
return;
}
}
private throw() {
throw new TooManyRequestsHttpError();
private throw(unjailedIn: number) {
throw new TooManyRequestsHttpError(unjailedIn);
}
}