Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Message to replace removed headers with in the request
var REMOVED_HEADER_MESSAGE = '[REMOVED BY SDK]';
// Range of SERVER ERROR http status codes
var HTTP_STATUS_CODE_SERVER_ERROR_BLOCK_RANGE = [
500,
599
];
// Timer used to track elapsed time beginning from executing an async request to emitting the response.
var asyncRequestTimer;
// A map of HTTP status codes and whether or not they can be retried
var retryableStatusCodes = {};
retryableStatusCodes[httpStatusCodes.REQUEST_TIMEOUT] = true;
retryableStatusCodes[httpStatusCodes.TOO_MANY_REQUESTS] = true;
// Retry intervals are between 50% and 150% of the exponentially increasing base amount
const RETRY_RANDOMIZATION_FACTOR = 0.5;
/**
* Returns true if the response info indicates a temporary/transient error.
*
* @param {?APIRequest~ResponseObject} response The response info from an API request,
* or undefined if the API request did not return any response info.
* @returns {boolean} True if the API call error is temporary (and hence can
* be retried). False otherwise.
* @private
*/
function isTemporaryError(response) {
var statusCode = response.statusCode;
entitiesToIndex = response.items.reduce((accumulator, item) => {
// We currently only handle queue overrun
if (item.index.status === httpStatus.TOO_MANY_REQUESTS) {
const failedEntity = entities.find(
(element) => element.bbid === item.index._id
);
accumulator.push(failedEntity);
}
return accumulator;
}, []);
exports.rateLimitHandler = (req, res, next) => {
const err = new APIError({
message: 'Rate limt exceeded, please try again later some time.',
status: httpStatus.TOO_MANY_REQUESTS,
});
return handler(err, req, res);
};
async function newModel(req: auth.RequestWithProject, res: Express.Response) {
switch (req.project.type) {
case 'text': {
try {
const model = await conversation.trainClassifier(req.project);
return res.status(httpstatus.CREATED).json(returnConversationWorkspace(model));
}
catch (err) {
if (err.message === conversation.ERROR_MESSAGES.INSUFFICIENT_API_KEYS) {
return res.status(httpstatus.CONFLICT).send({ code : 'MLMOD01', error : err.message });
}
else if (err.message === conversation.ERROR_MESSAGES.API_KEY_RATE_LIMIT) {
return res.status(httpstatus.TOO_MANY_REQUESTS).send({ code : 'MLMOD02', error : err.message });
}
else if (err.message === conversation.ERROR_MESSAGES.MODEL_NOT_FOUND) {
return res.status(httpstatus.NOT_FOUND)
.send({ code : 'MLMOD03', error : err.message + ' Please try again' });
}
else if (err.statusCode === httpstatus.UNAUTHORIZED) {
return res.status(httpstatus.CONFLICT)
.send({
code : 'MLMOD04',
error : 'The Watson credentials being used by your class were rejected. ' +
'Please let your teacher or group leader know.',
});
}
else if (err.message === 'Unexpected response when retrieving service credentials') {
return res.status(httpstatus.CONFLICT)
.send({