Skip to content

Commit

Permalink
Added support for handling throttling status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
verifalia committed Sep 26, 2019
1 parent b045068 commit 8d93983
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/MultiplexedRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { EndpointServerError } from './errors/EndpointServerError';
import { AuthorizationError } from './errors/AuthorizationError';
import { WellKnowMimeContentTypes } from './WellKnowMimeContentTypes';
import { LoggerFactory } from './environments/environment';
import { RequestThrottledError } from './errors/RequestThrottledError';

const loggerFactory = new LoggerFactory();
const logger = loggerFactory.build('verifalia');
Expand Down Expand Up @@ -78,15 +79,25 @@ export class MultiplexedRestClient {
continue;
}

// Internal server error HTTP 5xx

if (response.status >= 500 && response.status <= 599) {
errors.push(new EndpointServerError(`Endpoint ${baseUri} returned an HTTP ${response.status} status code.`));
continue;
}

// Authentication / authorization error

if (response.status === 401 || response.status === 403) {
throw new AuthorizationError(response.statusText);
}

// Throttling

if (response.status === 429) {
throw new RequestThrottledError();
}

return response;
}

Expand Down
11 changes: 11 additions & 0 deletions src/errors/RequestThrottledError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { VerifaliaError } from "./VerifaliaError";

/**
* Thrown in the event a request exceeded the maximum configured email validations rate or the maximum number
* of concurrent requests from the same IP address.
*/
export class RequestThrottledError extends VerifaliaError {
constructor() {
super(`The current request has been throttled; please try again later.`);
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ export { DailyUsageListingOptions } from './credits/models/DailyUsageListingOpti
export { Balance } from './credits/models/Balance';
export { DailyUsage } from './credits/models/DailyUsage';
export { Direction } from './common/Direction';
export { OperationCancelledError } from './errors/OperationCancelledError';
export { OperationCancelledError } from './errors/OperationCancelledError';
export { RequestThrottledError } from './errors/RequestThrottledError';
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// generated by genversion
export const version = '2.0.1'
export const version = '2.1.0'

0 comments on commit 8d93983

Please sign in to comment.