Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added error handling in index.js. #1058

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions scripts/converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,26 @@ const toVersion = '3.0.0';
*/
function convertExample(exampleFile) {
console.warn(`Converting: ${exampleFile}`);
const document = fs.readFileSync(exampleFile, 'utf-8');
const loadedDocument = jsYaml.load(document);
let document;

try {
document = fs.readFileSync(exampleFile, 'utf8');

} catch (error) {
console.error(`Error reading file ${exampleFile}:`, error);
return;

}

let loadedDocument;
try {
loadedDocument = jsYaml.load(document);

} catch (error) {
console.error(`Error parsing YAML for file ${exampleFile}:`, error);
return;

}
if(loadedDocument.asyncapi === undefined) {
//Probably encountered a common file (used in other files), ignore
console.error(`___________________________________________________________________________________
Expand All @@ -27,7 +45,13 @@ ________________________________________________________________________________
console.warn(`${exampleFile} is already version ${toVersion}`);
return;
}
const convertedDocument = convert(document, toVersion, { });
let convertedDocument;
try {
convertedDocument = convert(document, toVersion, { });
} catch (error) {
console.error(`Error converting file ${exampleFile}:`, error);
return;
}
fs.writeFileSync(exampleFile, convertedDocument);
}

Expand All @@ -37,7 +61,13 @@ ________________________________________________________________________________
* @param {*} directoryPath full path to a directory to convert examples from.
*/
async function convertExampleDir(directoryPath) {
let examplesFiles = await fs.promises.readdir(directoryPath);
let examplesFiles;
try {
examplesFiles = fs.readdir(directoryPath);
} catch (error) {
console.error(`Error reading directory ${directoryPath}:`, error);
return;
}
examplesFiles = examplesFiles.map((file) => path.resolve(directoryPath, file));
const nestedDirectory = examplesFiles.filter((file) => fs.lstatSync(file).isDirectory());
for (const dir of nestedDirectory) {
Expand All @@ -53,5 +83,9 @@ async function convertExampleDir(directoryPath) {
*
*/
(async () => {
await convertExampleDir(examplesDirectory);
try {
await convertExampleDir(examplesDirectory);
} catch (error) {
console.error('Error during conversion process:', error);
}
})()
Loading