Skip to content

Conversation

@Viole07
Copy link

@Viole07 Viole07 commented Oct 9, 2024


Pull Request Description :

Closes #1402

This pull request refactors and improves several scripts in the project by enhancing error handling, improving validation logic, and streamlining promise management. Below are the key changes made across different files:

  1. verifyExtension.js:

    • Refactored promise handling by introducing a try-catch block for consistent async error handling across the function.
    • Replaced the mix of .catch() and async/await with a more streamlined approach, ensuring any unhandled async errors are caught and logged properly.
    • Added detailed error logging to help with debugging during extension verification.
  2. extract-extension.js:

    • Refactored to streamline the promise handling by wrapping the entire function in a try-catch block.
    • This ensures that any errors related to loading or extracting the zip file are handled in a single place, providing more consistent error handling.
    • Improved the error logging by including the actual error message in the returned result for better context during debugging.
  3. ExtensionNameValidator.js:

    • Added a type check for extensionName to ensure it is a string before proceeding with further validation.
    • This prevents potential runtime errors when invalid data types are passed, making the function more robust.
  4. PascalCase.js:

    • Combined two similar loops into a single loop that processes both publicEventsFunctions and eventsBasedBehaviors. This reduces code duplication and simplifies the logic.
    • Added a type check for extension.name to ensure it is a string before validating the PascalCase, preventing errors when the name is not provided or is of the wrong type.

Benefits:

  • Improved code readability and maintainability by consolidating promise handling and reducing redundancy.
  • Increased robustness through added type checks and better error logging, making debugging easier.
  • Simplified logic by combining redundant loops and refactoring validation functions into smaller, reusable components.

@Viole07 Viole07 requested review from a team as code owners October 9, 2024 14:07
@Viole07
Copy link
Author

Viole07 commented Oct 9, 2024

hey @4ian , why is this check not passing.. i tried fixing the issue using

npx prettier --write "scripts//*.js" "scripts//*.ts"

and it's still not working..

Comment on lines +40 to +44
// Combine both loops into one to reduce duplication
const allFunctions = [...publicEventsFunctions, ...eventsBasedBehaviors];
for (const { name } of allFunctions) {
checkPascalCase(name, onError);
}
Copy link
Member

Choose a reason for hiding this comment

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

This offers no new benefit, does an inefficient copy, and unnecessarily complexifies the logic.

Comment on lines +33 to +36
if (typeof name !== 'string') {
onError('Invalid extension name. Expected a string.');
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

Typechecking is already statically enforced via typescript: this is unnecessary complexity.

Comment on lines +3 to +5
// Ensure extensionName is a string to prevent runtime errors
if (typeof extensionName !== 'string') return false;

Copy link
Member

Choose a reason for hiding this comment

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

Typechecking is already statically enforced via typescript: this is unnecessary complexity.

Comment on lines 18 to +43
try {
// Load in the archive with JSZip
const zip = await JSZip.loadAsync(await readFile(zipPath));
if (!zip) return { error: 'zip-error' };
// Find JSON files in the zip top level folder
const jsonFiles = zip.file(/.*\.json$/);
// Ensure there is exactly 1 file
if (jsonFiles.length === 0) return { error: 'no-json-found' };
if (jsonFiles.length > 1) return { error: 'too-many-files' };
const [file] = jsonFiles;
const { name: extensionName, dir, ext } = parsePath(file.name);
if (ext !== '.json') return { error: 'invalid-file-name' };
// Ensure no special characters are in the extension name to prevent relative path
// name shenanigans with dots that could put the extension in the reviewed folder.
if (dir) return { error: 'invalid-file-name' };
if (!isValidExtensionName(extensionName))
return { error: 'invalid-file-name' };
// Write the extension to the community extensions folder
await pipeline(
file.nodeStream(),
createWriteStream(`${extensionsFolder}/community/${file.name}`)
);
return { extensionName };
} catch (e) {
console.warn(`JSZip extraction error caught: `, e);
return { error: 'zip-error' };
console.warn(`JSZip loading or extraction error caught: `, e);
return { error: 'zip-error', details: e.message };
Copy link
Member

Choose a reason for hiding this comment

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

The try catch block should only cover the block it is handling errors for (the JSZip.loadAsync call)

} catch (error) {
// General error catch for any unhandled async errors in the process
console.error(`Error verifying extension: ${extensionName}`, error);
return { code: 'not-found' }; // Return a default error code, you could also return something more specific based on the context
Copy link
Member

Choose a reason for hiding this comment

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

Most definitely not! The error code determines what error message is sent back to the submitter - sending them a message about a file not being found is not generic, and goes against the point of providing feedback. The script crashing is already a handled case, so this unnecessarily strips the context of the error.

@arthuro555
Copy link
Member

Since every proposed change here does not in fact improve promise handling, validation or code efficiency, I'll close this PR.

@arthuro555 arthuro555 closed this Dec 15, 2024
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.

Refactor and Bug Fixes for Promise Handling and Validation

2 participants