@salesforce/salesforcedx-vscode-test-tools
Version:
Test automation framework for Salesforce Extensions for VS Code
67 lines • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.retryOperation = exports.verifyNotificationWithRetry = void 0;
/*
* Copyright (c) 2025, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const core_1 = require("./core");
const ui_interaction_1 = require("./ui-interaction");
/**
* Retry a notification check
* @param notificationPattern - The notification pattern to check
* @param wait - The wait time for the notification to appear
* @param methodToRunForEachTry - The method to run for each try
* @returns
*/
const verifyNotificationWithRetry = async (notificationPattern, wait = core_1.Duration.minutes(3), methodToRunForEachTry) => {
return await (0, exports.retryOperation)(async () => {
if (methodToRunForEachTry) {
await methodToRunForEachTry();
}
const notificationWasFound = await (0, ui_interaction_1.notificationIsPresentWithTimeout)(notificationPattern, wait);
if (!notificationWasFound) {
(0, core_1.log)(`Notification ${notificationPattern} was not found`);
await (0, ui_interaction_1.executeQuickPick)('Notifications: Show Notifications');
throw new Error(`Notification ${notificationPattern} was not found`);
}
return notificationWasFound;
}, 5, `Failed to find notification ${notificationPattern}`);
};
exports.verifyNotificationWithRetry = verifyNotificationWithRetry;
/**
* Retry an operation
* @param operation - The operation to retry
* @param maxAttempts - The maximum number of attempts
* @param errorMessage - The error message to log
* @returns The result of the operation
*/
const retryOperation = async (operation, maxAttempts = 3, errorMessage = 'Operation failed') => {
const formatError = (error) => {
if (error instanceof Error) {
return `${error.message}${error.stack ? '\nStack trace:\n' + error.stack : ''}`;
}
if (typeof error === 'string') {
return error;
}
return JSON.stringify(error);
};
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await operation();
}
catch (error) {
const formattedError = formatError(error);
if (attempt === maxAttempts) {
(0, core_1.log)(`${errorMessage} - Final attempt failed: ${formattedError} (after ${maxAttempts} attempts)`);
throw error;
}
(0, core_1.log)(`${errorMessage} - Attempt ${attempt}/${maxAttempts} failed: ${formattedError}, trying again...`);
}
}
throw new Error(`${errorMessage} after ${maxAttempts} attempts`);
};
exports.retryOperation = retryOperation;
//# sourceMappingURL=retryUtils.js.map