/**
* Detects errors in task queues for staging environments (synchronous).
* Assumes a simple queue structure (e.g., an array).
*
* @param {Array} queue - The task queue (array of task objects).
* @param {function} taskExecutor - A function to execute each task. Receives a task object as input.
* @returns {Array} - An array of error objects. Empty array if no errors found.
*/
function detectStagingQueueErrors(queue, taskExecutor) {
const errors = [];
if (!Array.isArray(queue)) {
errors.push({ message: "Queue must be an array." });
return errors;
}
for (let i = 0; i < queue.length; i++) {
const task = queue[i];
if (typeof task !== 'object' || task === null) {
errors.push({ message: `Invalid task at index ${i}. Task must be an object.` });
continue; // Skip to the next task
}
if (typeof taskExecutor !== 'function') {
errors.push({ message: "taskExecutor must be a function." });
return errors;
}
try {
taskExecutor(task); // Execute the task
} catch (error) {
errors.push({ index: i, message: error.message, stack: error.stack }); // Capture error details
}
}
return errors;
}
export default detectStagingQueueErrors;
Add your comment