@cumulus/aws-client
Version:
Utilities for working with AWS
72 lines • 2.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.retryOnThrottlingException = exports.improveStackTrace = exports.setErrorStack = exports.getServiceIdentifer = void 0;
const p_retry_1 = __importDefault(require("p-retry"));
const errors_1 = require("@cumulus/errors");
const getServiceIdentifer = (service) => service.serviceIdentifier || service.name;
exports.getServiceIdentifer = getServiceIdentifer;
// Replace the stack of an error
// Note: This mutates the error that was passed in.
const setErrorStack = (error, newStack) => {
if (error.stack) {
// eslint-disable-next-line no-param-reassign
error.stack = [
error.stack.split('\n')[0],
...newStack.split('\n').slice(1),
].join('\n');
}
else {
// eslint-disable-next-line no-param-reassign
error.stack = newStack;
}
};
exports.setErrorStack = setErrorStack;
// eslint-disable-next-line valid-jsdoc
/**
* Wrap a function and provide a better stack trace
*
* If a call is made to the aws-sdk and it causes an exception, the stack trace
* that is returned gives no indication of where the error actually occurred.
*
* This utility will wrap a function and, when it is called, update any raised
* error with a better stack trace.
*
* @private
*/
const improveStackTrace = (fn) => async (...args) => {
const tracerError = new Error();
try {
Error.captureStackTrace(tracerError);
return await fn(...args);
}
catch (error) {
// @ts-ignore
(0, exports.setErrorStack)(error, tracerError.stack);
error.message = `${error.message}; Function params: ${JSON.stringify(args, undefined, 2)}`;
throw error;
}
};
exports.improveStackTrace = improveStackTrace;
const retryIfThrottlingException = (err) => {
if ((0, errors_1.isThrottlingException)(err))
throw err;
throw new p_retry_1.default.AbortError(err);
};
/**
* Wrap a function so that it will retry when a ThrottlingException is encountered.
*
* @param {Function} fn - the function to retry. This function must return a Promise.
* @param {Object} options - retry options, documented here:
* - https://github.com/sindresorhus/p-retry#options
* - https://github.com/tim-kos/node-retry#retryoperationoptions
* - https://github.com/tim-kos/node-retry#retrytimeoutsoptions
* @returns {Function} a function that will retry on a ThrottlingException
*
* @private
*/
const retryOnThrottlingException = (fn, options = {}) => (...args) => (0, p_retry_1.default)(() => fn(...args).catch(retryIfThrottlingException), { maxTimeout: 5000, ...options });
exports.retryOnThrottlingException = retryOnThrottlingException;
//# sourceMappingURL=utils.js.map