Skip to content

Commit 349d2c4

Browse files
committed
[Fix] Panic when using npm start on OSX - fresh dev setup.
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.
1 parent e190610 commit 349d2c4

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

app/public/js/common/configLocation.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,26 @@ const configuration = {
3838
userConfigPath = `${userHome}/Library/Preferences/Soundnode`;
3939
}
4040

41+
// Guard to assert type of string.
42+
if (typeof userConfigPath !== "string") {
43+
throw `Could not set userConfigPath for this OS ${process.platform}`
44+
}
45+
4146
// create user config in path
4247
// if there is no userConfig path
43-
if (!fs.statSync(userConfigPath).isDirectory()) {
44-
this.createUserConfig()
48+
//
49+
// fs.statSync will throw an exception if
50+
// any directory along the path doesn't exist.
51+
// Solution: use try catch.
52+
try {
53+
var fi = fs.statSync(userConfigPath);
54+
if (!fi.isDirectory()) {
55+
this.createUserConfig(userConfigPath);
56+
}
57+
}
58+
catch(error) {
59+
this.createUserConfig(userConfigPath);
4560
}
46-
4761
return userConfigPath;
4862
},
4963

0 commit comments

Comments
 (0)