Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const req = httpModule.request(this._getRequestOptions(), (res: http.IncomingMessage) => {
const statusCode = res.statusCode || 500;
const status = Status.fromHttpCode(statusCode);
res.setEncoding('utf8');
if (status === Status.Success) {
resolve({ status });
} else {
if (status === Status.RateLimit) {
const now = Date.now();
let header = res.headers ? res.headers['Retry-After'] : '';
header = Array.isArray(header) ? header[0] : header;
this._disabledUntil = new Date(now + parseRetryAfterHeader(now, header));
logger.warn(`Too many requests, backing off till: ${this._disabledUntil}`);
}
let rejectionMessage = `HTTP Error (${statusCode})`;
if (res.headers && res.headers['x-sentry-error']) {
rejectionMessage += `: ${res.headers['x-sentry-error']}`;
}
reject(new SentryError(rejectionMessage));
.then(response => {
const status = Status.fromHttpCode(response.status);
if (status === Status.Success) {
resolve({ status });
return;
}
if (status === Status.RateLimit) {
const now = Date.now();
this._disabledUntil = new Date(now + parseRetryAfterHeader(now, response.headers.get('Retry-After')));
logger.warn(`Too many requests, backing off till: ${this._disabledUntil}`);
}
reject(response);
})
.catch(reject);
request.onreadystatechange = () => {
if (request.readyState !== 4) {
return;
}
const status = Status.fromHttpCode(request.status);
if (status === Status.Success) {
resolve({ status });
return;
}
if (status === Status.RateLimit) {
const now = Date.now();
this._disabledUntil = new Date(now + parseRetryAfterHeader(now, request.getResponseHeader('Retry-After')));
logger.warn(`Too many requests, backing off till: ${this._disabledUntil}`);
}
reject(request);
};