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] Panic when using npm start on OSX fresh dev setup. Resolves #1017. #1016

Merged
merged 4 commits into from
Jan 5, 2018

Conversation

JackMordaunt
Copy link
Contributor

@JackMordaunt JackMordaunt commented Oct 6, 2017

Panic occurs when the path ~/Library/Preferences/Soundnode does not exist (ie a fresh dev setup) since getUserConfig() uses fs.statSync(..), which throws an exception instead of returning an error if the directory does not exist.

If the directory mentioned above does exist, all is fine. When it doesn't, such as on a fresh dev setup, every time you run 'npm start' you get a panic and the app crashes.

My solution to this is to wrap fs.statSync(..) in a try catch and problem solved.

Furthermore I added in a guard which checks the type of userConfigPath. The reason for this guard is so we can throw a useful exception rather than a generic undefined or null exception.

Panic occurs when the path ~/Library/Preferences/Soundnode does not exist (ie a fresh dev setup) since `getUserConfig()` uses `fs.statSync(..)`, which throws an exception instead of returning an error if the directory does not exist.

If the directory mention above does exist, all is fine. When it doesn't, such as on a fresh dev setup, every time you run 'npm start' you get a panic and the app crashes.

My solution to this is to wrap `fs.statSync(..)` in a try catch and problem solved.

Furthermore I added in a guard which checks the type of `userConfigPath`. The reason for this guard is so we can throw a useful exception rather than a generic undefined or null exception.
@JackMordaunt JackMordaunt changed the title [Fix] Panic when using npm start on OSX fresh dev setup. [Fix] Panic when using npm start on OSX fresh dev setup. Resolves #1017. Oct 6, 2017
// any directory along the path doesn't exist.
// Solution: use try catch.
try {
var fi = fs.statSync(userConfigPath);
Copy link
Member

Choose a reason for hiding this comment

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

can you rename the variable to something more clear please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given the tiny scope of the variable I felt the name to be adequate.
What would you consider to be a more clear name? Maybe something like configFileInfo?

Copy link
Member

Choose a reason for hiding this comment

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

configFileInfo would be more explicit,

Since the variable is never mutated, could you change it to a constant?

@@ -38,12 +38,26 @@ const configuration = {
userConfigPath = `${userHome}/Library/Preferences/Soundnode`;
}

// Guard to assert type of string.
if (typeof userConfigPath !== "string") {
Copy link
Member

Choose a reason for hiding this comment

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

can you tell me if there is a specific case for this check to be necessary? thanks

Copy link
Contributor Author

@JackMordaunt JackMordaunt Jan 2, 2018

Choose a reason for hiding this comment

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

Since userConfigPath is initially set to null, if the platform is not linux, windows, or darwin, then it never gets assigned to a string. Thus if userConfigPath is still a null value the platform is obviously not supported, so an exception is thrown to let the user know why the userConfigPath value could not be set properly.

The bug was actually quite hard to find because all I had to go on was a generic null reference exception, this guard makes the exception explicitly clear.

// fs.statSync will throw an exception if
// any directory along the path doesn't exist.
// Solution: use try catch.
try {
Copy link
Member

Choose a reason for hiding this comment

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

what are other solutions instead try catch?

Copy link
Member

@jakejarrett jakejarrett Jan 1, 2018

Choose a reason for hiding this comment

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

@weblancaster It appears stat throws an error, so we will need to catch the error or rethink the entire functionality of the configLocation

EDIT: nevermind, the callback returns an error variable.

It might be better to be safe so the startup of the application doesn't fail though.

@weblancaster
Copy link
Member

@JackMordaunt thanks for contributing and sorry for my delay in reply to you.

can you take a look the comments I left please? thanks.

@jakejarrett
Copy link
Member

this looks good to me, any concerns about merging @weblancaster ?

@jakejarrett jakejarrett added this to the 7.0.0 milestone Jan 5, 2018
@jakejarrett
Copy link
Member

@weblancaster Going to merge this so we can get a new release out.

@jakejarrett jakejarrett merged commit 61542fd into Soundnode:master Jan 5, 2018
@JackMordaunt JackMordaunt deleted the config-panic-fix branch January 5, 2018 03:46
@weblancaster
Copy link
Member

@jakejarrett the code looks good but have you test before merging? if so and worked as expected no worries.
Otherwise will are taking a risk without testing.

@jakejarrett
Copy link
Member

Yeah tested on Windows, Linux & Mac OSX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants