facebook / memlab

A framework for finding JavaScript memory leaks and analyzing heap snapshots
https://facebook.github.io/memlab/
MIT License
4.3k stars 116 forks source link

How to handle error or exception if one of script file stop execution? #84

Closed rizwanansari367 closed 1 year ago

rizwanansari367 commented 1 year ago

Hi, I have written a script that will execute all scenario files and store the result in separate directory. But if one of the scenario file failed to execute then it stopped the execution of the remaining scenario file also. I am handling the exception in my main script file with try catch around run, but not working. Below is the main script file.

const { run } = require('@memlab/api'); const fs = require('fs-extra'); const path = require('path');

const scenariosDir = 'D:/Puppeteer'; const workDir = 'D:/Puppeteer/Result';

// Read all scenario files from the Scenarios directory const scenarioFiles = fs.readdirSync(scenariosDir).filter(file => file.endsWith('.js'));

(async function () { // Ensure the working directory exists fs.mkdirsSync(workDir);

// Iterate through all scenario files and run them for (const scenarioFile of scenarioFiles) { const scenario = require(path.join(scenariosDir, scenarioFile)); const scenarioResultDir = path.join(workDir, path.basename(scenarioFile, '.js'));

fs.mkdirsSync(scenarioResultDir);

  try {// Run the scenario and store the result
    const result = await run({scenario, workDir: scenarioResultDir});
    console.log(result);
  }catch (e) {
    console.log(`Error due to scenario "${scenario}"`);
  }
   //Skip to the next iteration if the scenario failed to execute
  if (result && result.errorMessage) {
    console.log(`Skipping scenario "${scenarioFile}" due to error: ${result.errorMessage}`);
    continue; // Continue with the next scenario file
  }

}

})();

JacksonGL commented 1 year ago

There is currently limited support for throwing exception in scenario execution. I may make a few patches when I get a chance. If the exception is thrown from your scenario callbacks (e.g., action, back etc.), I recommend adding try-catch clauses in your scenario callbacks. An alternative is to initiate a memlab run via the bash command (via the node.js child process API).