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

Add try/catch when writing yarn-error.log #2095

Closed
wants to merge 2 commits into from
Closed

Add try/catch when writing yarn-error.log #2095

wants to merge 2 commits into from

Conversation

androa
Copy link

@androa androa commented Nov 30, 2016

Summary
If yarn is lacking write access, the fs.writeFileSync() method throws an
uncaught exception that blocks further execution.

Simply catching it and allowing further exection provides desired behaviour by
giving the user feedback and having yarn terminate.

Test plan

mkdir -m 500 bar && cd bar
yarn init -y

now results in

node@062ea1ba7c46:~/bar$ /yarn/bin/yarn init -y
yarn init v0.18.0-0
warning The yes flag has been set. This will automatically answer yes to all questions which may have security implications.
success Saved package.json
error An unexpected error occurred: "EACCES: permission denied, open '/home/node/bar/package.json'".
info If you think this is a bug, please open a bug report with the information provided in "/home/node/bar/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/init for documentation about this command.

Previously it just kept hanging forever.

Fixes #2094

If yarn is lacking write access, the `fs.writeFileSync()` method throws an
uncaught exception that blocks further execution.

Simply catching it and allowing further exection provides desired behaviour by
giving the user feedback and having yarn terminate.

Fixes #2094
fs.writeFileSync(errorLoc, log.join('\n\n') + '\n');
try {
fs.writeFileSync(errorLoc, log.join('\n\n') + '\n');
} catch (err) {}
Copy link
Member

Choose a reason for hiding this comment

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

This should probably at least console.error or something rather than silently ignoring it.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like reporter.error('Could not write yarn-error.log: ' + err) (with proper localized string)

Copy link
Author

Choose a reason for hiding this comment

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

The thing is, #346 already handles it marvelously. It just needs to get there. I was thinking to rather piggyback on that one, rather than handling it explicitly. But I can of course change into doing so.

reporter.info(reporter.lang('bugReport', errorLoc));
} catch (err) {
if (err.code === 'EACCES') {
reporter.error(reporter.lang('noFilePermission', err.path));
Copy link
Author

Choose a reason for hiding this comment

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

@Daniel15 About doing this instead then?

Copy link
Member

Choose a reason for hiding this comment

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

We don't use console.error anywhere, better to keep to reporter.error anyway.
How about you wrap only fs.writeFileSync into try/catch?

Copy link
Member

@bestander bestander left a comment

Choose a reason for hiding this comment

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

  • no need for console.error
  • try/catch too broad

@androa
Copy link
Author

androa commented Jan 23, 2017

Seems to have been solved by #1592 and this can be closed.

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.

3 participants