From 7db6c0e0c76a17559a5008823d5233665c5558e2 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Fri, 24 Apr 2020 11:17:58 +0200 Subject: [PATCH] Add retry in indication to TooManyRequests http error --- package.json | 2 +- src/HttpError.ts | 4 ++-- src/Throttler.ts | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 56b0fd4..5dd39b6 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/HttpError.ts b/src/HttpError.ts index 27263f4..3e7b314 100644 --- a/src/HttpError.ts +++ b/src/HttpError.ts @@ -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 ); diff --git a/src/Throttler.ts b/src/Throttler.ts index b802541..2996ad0 100644 --- a/src/Throttler.ts +++ b/src/Throttler.ts @@ -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); } } \ No newline at end of file