Skip to content

Fix usage example scraper error handling - #804

Open
Osaid2993 wants to merge 1 commit into
thoth-tech:mainfrom
Osaid2993:fix/usage-example-scraper-validation
Open

Fix usage example scraper error handling#804
Osaid2993 wants to merge 1 commit into
thoth-tech:mainfrom
Osaid2993:fix/usage-example-scraper-validation

Conversation

@Osaid2993

Copy link
Copy Markdown

Description

This pull request fixes error handling in the usage-example scraping script.

Previously, when a Python usage example was missing its matching .txt description file, the scraper caught the error too broadly, skipped the affected category, wrote incomplete reference data, printed a success message, and exited with status code 0.

The updated scraper now:

  • validates that each Python usage example has a matching .txt description file
  • reports the exact missing file path
  • stops processing when validation fails
  • avoids overwriting the existing generated JSON after a failure
  • prints the success message only after successful completion
  • returns a non-zero exit code when scraping fails
  • declares the Python file collection locally instead of creating undeclared variables

No new dependencies are required.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

The successful workflow was tested by running:

node scripts/usage-example-scraping.cjs

echo "exit=$?"

The script completed successfully and returned:

All examples have been scraped successfully.

exit=0

The normal repository generation workflow was also tested with:

npm run generate-json

The failure behaviour was tested by temporarily renaming an existing .txt description file while leaving its matching Python example in place.

The scraper correctly:

  • identified the exact missing description file
  • printed a clear failure message
  • returned exit=1
  • did not print the success message
  • preserved the existing usage-example-references.json file

The output JSON was checked before and after the failure using shasum, and both checksums were identical.

Syntax and formatting checks were also run:

node --check scripts/usage-example-scraping.cjs

git diff --check

Both completed without errors.

Testing Checklist

  • Tested in latest Chrome
  • Tested in latest Firefox
  • npm run build
  • npm run preview

Checklist

If involving code

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

Folders and Files Added/Modified

  • Modified:
    • scripts/usage-example-scraping.cjs

Additional Notes

Generated JSON files were restored after testing so this pull request remains focused only on the scraper error-handling fix.

This change improves the reliability of the usage-example generation workflow and gives contributors actionable feedback when an example is incomplete, rather than allowing incomplete reference data to be generated silently.

@netlify

netlify Bot commented Aug 3, 2026

Copy link
Copy Markdown

Deploy Preview for splashkit failed.

Name Link
🔨 Latest commit 8f338d3
🔍 Latest deploy log https://app.netlify.com/projects/splashkit/deploys/6a707ce3b18abb000842903a

@ralphweng2023 ralphweng2023 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran both versions over the current tree. The generated usage-example-references.json comes out byte identical, so this is pure error handling with no output drift. Also reproduced the failure case by leaving a .py with no matching .txt: the old script still says all examples were scraped and exits 0, this one names the missing file and exits 1.

Good catch on the two implicit globals as well. pythonFiles and textFiles were both leaking to global scope. Anchoring the .py to .txt replace is the quiet fix I like most here, the unanchored version would rewrite the wrong part of any path with .py earlier in it. Approved.

@ayushsdeakin ayushsdeakin left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Duplicate declarations: A few variables (pythonFiles, pythonFile, textFile, funcEntry, etc.) are declared more than once, which will cause errors. Might be worth cleaning these up.
  2. Unused variable: textFiles is declared but doesn't seem to be used anywhere. You could probably remove it.
  3. Filename validation: It might be worth checking if pyFileMatch exists before accessing pyFileMatch[1], otherwise an invalid filename could cause the script to crash.

const files = fs.readdirSync(folderPath);
// Filtering for JSON files
pythonFiles = files.filter(file => path.extname(file).toLowerCase() === '.py');
textFiles = files.filter(file => path.extname(file).toLowerCase() === '.txt');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

textFiles is created but never used please consder removing it if it isn't required.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm...This line is part of the removed code (red highlight), it's being deleted in this PR. The updated version on the green side no longer declares textFiles.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I was reviewing the diff as a whole rather than just the added lines, so I ended up commenting on code that was already on code. running the actual updated code now

const pythonFile = fs.readFileSync(pythonPath, "utf8");
const textFile = fs.readFileSync(textPath, "utf8");
const title = textFile.split("\n")[0];
const pyFileMatch = fileNameRegex.exec(pyFile);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code uses pyFileMatch[1] without checking whether fileNameRegex.exec(pyFile) actually returned a match. If the filename is invalid,and also this could throw an error. Add validation before accessing the array.

const pyFileMatch = fileNameRegex.exec(pyFile);
try {

try {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested try/catch blocks make the code hard to follow.

@ralphweng2023 ralphweng2023 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approval at db6f838. The three issues ayushsdeakin raised, namely the duplicate pythonFiles/textFiles declarations across folder iterations, the unused textFiles variable, and the missing filename validation, are all addressed in this diff. pythonFiles is now scoped inside the folder loop, textFiles is gone, and the existsSync check on textPath throws a clear error naming both the missing source and the expected companion. Errors now propagate via throw and process.exitCode = 1 rather than being swallowed, and the array-vs-file log message is fixed. Approved.

@rachelpatrao rachelpatrao left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! The scraper now handles missing description files correctly, reports clear errors, and avoids generating incomplete reference data when validation fails. The changes are well tested and everything looks good from my side. Happy to approve!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants