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", "name": "wms-core",
"version": "0.2.10", "version": "0.2.11",
"description": "Node web framework", "description": "Node web framework",
"repository": "git@gitlab.com:ArisuOngaku/wms-core.git", "repository": "git@gitlab.com:ArisuOngaku/wms-core.git",
"author": "Alice Gaudon <alice@gaudon.pro>", "author": "Alice Gaudon <alice@gaudon.pro>",

View File

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

View File

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