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

refactor(v2): add exception handling if external command is fails #4870

Merged
merged 2 commits into from
Jun 3, 2021

Conversation

lex111
Copy link
Contributor

@lex111 lex111 commented May 30, 2021

Motivation

Related to #4844.

In fact, we have exception handler for internal commands, so I think it makes sense to do something similar for external commands.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Not yet possible

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@lex111 lex111 added the pr: polish This PR adds a very minor behavior improvement that users will enjoy. label May 30, 2021
@lex111 lex111 requested a review from slorber as a code owner May 30, 2021 08:19
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 30, 2021
@lex111 lex111 changed the title refactor(v2): add handle exception if external command is fails refactor(v2): add exception handling if external command is fails May 30, 2021
@netlify
Copy link

netlify bot commented May 30, 2021

✔️ [V2]

🔨 Explore the source changes: 0953226

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/60b7d5283b293e0007eec1f9

😎 Browse the preview: https://deploy-preview-4870--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented May 30, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 55
🟢 Accessibility 97
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-4870--docusaurus-2.netlify.app/

@netlify
Copy link

netlify bot commented May 30, 2021

✔️ [V1]

🔨 Explore the source changes: 0953226

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-1/deploys/60b7d52848515400073f7b1a

😎 Browse the preview: https://deploy-preview-4870--docusaurus-1.netlify.app

@github-actions
Copy link

github-actions bot commented May 30, 2021

Size Change: +1.08 kB (0%)

Total Size: 621 kB

Filename Size Change
website/build/assets/css/styles.********.css 88.3 kB +164 B (0%)
website/build/assets/js/main.********.js 443 kB +513 B (0%)
website/build/blog/2017/12/14/introducing-docusaurus/index.html 62.3 kB +201 B (0%)
website/build/index.html 27.1 kB +201 B (+1%)
ℹ️ View Unchanged
Filename Size Change
website/build/docs/introduction/index.html 235 B 0 B

compressed-size-action


if (!isInternalCommand(commandName)) {
try {
await externalCommand(cli, path.resolve('.'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not 100% sure but I think this does not really execute the command, it just registers it by loading plugin commands (otherwise we skip this process as an optimisation)

I think cli.parse(process.argv); is what executes the actual external command, so it should rather be added to the try/catch

Copy link
Collaborator

Choose a reason for hiding this comment

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

btw we should probably use cli.parseAsync instead of cli.parse

Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.

This way we wouldn't need to wrap each internal command with error handling logic, and just add a single try/catch around cli.parseAsync once.

I think with cli.parse it's not possible to catch the command execution error (it returns a promise instead of throwing), that's why wrapCommand was applied everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're right, cli.parse actually executes command, however wrap around this call with try-catch handles error for external command only, but internal commands still needs its own wrapper 🤷‍♂️

Therefore, I decided to use unhandledRejection event, which fires for all commands.

} catch (err) {
console.log(
chalk.red(
`Running external '${commandName}' command failed.\n\n${err}`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think users will understand the concept of "external command" so it's probably useless to mention it here?

@slorber
Copy link
Collaborator

slorber commented Jun 3, 2021

Thanks

Not sure it's the perfect solution yet, but at least it works in both cases so we can merge it for now

@slorber slorber merged commit 85e87b5 into master Jun 3, 2021
@slorber slorber deleted the lex111/handle-error-external-command branch August 17, 2021 18:03
taejs added a commit to taejs/docusaurus that referenced this pull request Mar 6, 2022
Josh-Cena pushed a commit that referenced this pull request Mar 6, 2022
* refactor(create): add exception handling if external command is fails

refers #4870

85e87b5

* fix(create): load entry file after node version checking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: polish This PR adds a very minor behavior improvement that users will enjoy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants