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

fix: Simplify-error-handling(#3305) #3310

Closed
Closed
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
23 changes: 14 additions & 9 deletions scripts/dashboard/build-dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,26 @@
};
} catch (e) {
console.error(
`there was some issues while parsing this item: ${JSON.stringify(
`There was some issues while parsing this item: ${JSON.stringify(
discussion
)}`
);
throw e;
throw e; // Throw the error instead of logging

Check warning on line 52 in scripts/dashboard/build-dashboard.js

View check run for this annotation

Codecov / codecov/patch

scripts/dashboard/build-dashboard.js#L52

Added line #L52 was not covered by tests
}
})
);
result.sort((ElemA, ElemB) => ElemB.score - ElemA.score);
const filteredResult = result.filter(issue => issue.author !== 'asyncapi-bot');
return filteredResult.slice(0, 12);
}

async function writeToFile(content) {
writeFileSync(
resolve(__dirname, '..', '..', 'dashboard.json'),
JSON.stringify(content, null, ' ')
);
}

Comment on lines +60 to +67
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Use asynchronous file write operation for non-blocking I/O

In the writeToFile async function, you're using writeFileSync, which is synchronous and blocks the event loop. To keep the code non-blocking and efficient, consider using the asynchronous version fs.promises.writeFile.

Apply this diff to fix the issue:

-const { writeFileSync } = require('fs');
+const { writeFile } = require('fs/promises');

...

-async function writeToFile(content) {
-  writeFileSync(
+async function writeToFile(content) {
+  await writeFile(
     resolve(__dirname, '..', '..', 'dashboard.json'),
     JSON.stringify(content, null, '  ')
   );
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async function writeToFile(content) {
writeFileSync(
resolve(__dirname, '..', '..', 'dashboard.json'),
JSON.stringify(content, null, ' ')
);
}
const { writeFile } = require('fs/promises');
async function writeToFile(content) {
await writeFile(
resolve(__dirname, '..', '..', 'dashboard.json'),
JSON.stringify(content, null, ' ')
);
}

async function mapGoodFirstIssues(issues) {
return issues.map((issue) => ({
id: issue.id,
Expand All @@ -87,7 +89,6 @@
return result && result.name.split('/')[1];
}


function monthsSince(date) {
const seconds = Math.floor((new Date() - new Date(date)) / 1000);
// 2592000 = number of seconds in a month = 30 * 24 * 60 * 60
Expand All @@ -112,7 +113,7 @@
`limit = ${result.rateLimit.limit}`,
`remaining = ${result.rateLimit.remaining}`,
`resetAt = ${result.rateLimit.resetAt}`
)
);
}

const hasNextPage = result.search.pageInfo.hasNextPage;
Expand All @@ -126,23 +127,25 @@
}
} catch (e) {
console.error(e);
throw e; // Throw the error instead of logging

Check warning on line 130 in scripts/dashboard/build-dashboard.js

View check run for this annotation

Codecov / codecov/patch

scripts/dashboard/build-dashboard.js#L130

Added line #L130 was not covered by tests
}
}

async function getDiscussionByID(isPR, id) {
try {
let result = await graphql(isPR ? Queries.pullRequestById : Queries.issueById, {
id,
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`,
},

}
);
});
return result;
} catch (e) {
console.error(e);
throw e; // Throw the error instead of logging

Check warning on line 145 in scripts/dashboard/build-dashboard.js

View check run for this annotation

Codecov / codecov/patch

scripts/dashboard/build-dashboard.js#L145

Added line #L145 was not covered by tests
}
}

async function start() {
try {
const [issues, PRs, rawGoodFirstIssues] = await Promise.all([
Expand All @@ -157,10 +160,12 @@
]);
writeToFile({ hotDiscussions, goodFirstIssues });
} catch (e) {
console.log('There were some issues parsing data from github.')
console.log('There were some issues parsing data from github.');

Check warning on line 163 in scripts/dashboard/build-dashboard.js

View check run for this annotation

Codecov / codecov/patch

scripts/dashboard/build-dashboard.js#L163

Added line #L163 was not covered by tests
console.log(e);
throw e; // Throw the error instead of logging

Check warning on line 165 in scripts/dashboard/build-dashboard.js

View check run for this annotation

Codecov / codecov/patch

scripts/dashboard/build-dashboard.js#L165

Added line #L165 was not covered by tests
}
}

start();

module.exports = { getLabel, monthsSince, mapGoodFirstIssues, getHotDiscussions, getDiscussionByID }
module.exports = { getLabel, monthsSince, mapGoodFirstIssues, getHotDiscussions, getDiscussionByID };

Check warning on line 171 in scripts/dashboard/build-dashboard.js

View check run for this annotation

Codecov / codecov/patch

scripts/dashboard/build-dashboard.js#L171

Added line #L171 was not covered by tests
Loading