1. /**
  2. * Detects errors in task queues for staging environments (synchronous).
  3. * Assumes a simple queue structure (e.g., an array).
  4. *
  5. * @param {Array} queue - The task queue (array of task objects).
  6. * @param {function} taskExecutor - A function to execute each task. Receives a task object as input.
  7. * @returns {Array} - An array of error objects. Empty array if no errors found.
  8. */
  9. function detectStagingQueueErrors(queue, taskExecutor) {
  10. const errors = [];
  11. if (!Array.isArray(queue)) {
  12. errors.push({ message: "Queue must be an array." });
  13. return errors;
  14. }
  15. for (let i = 0; i < queue.length; i++) {
  16. const task = queue[i];
  17. if (typeof task !== 'object' || task === null) {
  18. errors.push({ message: `Invalid task at index ${i}. Task must be an object.` });
  19. continue; // Skip to the next task
  20. }
  21. if (typeof taskExecutor !== 'function') {
  22. errors.push({ message: "taskExecutor must be a function." });
  23. return errors;
  24. }
  25. try {
  26. taskExecutor(task); // Execute the task
  27. } catch (error) {
  28. errors.push({ index: i, message: error.message, stack: error.stack }); // Capture error details
  29. }
  30. }
  31. return errors;
  32. }
  33. export default detectStagingQueueErrors;

Add your comment