From 349d2c488f97a0812f5be3dfdda3d0b6c878ea5a Mon Sep 17 00:00:00 2001 From: Jack Mordaunt Date: Fri, 6 Oct 2017 16:10:39 +1300 Subject: [PATCH 1/4] [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. --- app/public/js/common/configLocation.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js index 8dda7e44..b9e385c7 100644 --- a/app/public/js/common/configLocation.js +++ b/app/public/js/common/configLocation.js @@ -38,12 +38,26 @@ const configuration = { userConfigPath = `${userHome}/Library/Preferences/Soundnode`; } + // Guard to assert type of string. + if (typeof userConfigPath !== "string") { + throw `Could not set userConfigPath for this OS ${process.platform}` + } + // create user config in path // if there is no userConfig path - if (!fs.statSync(userConfigPath).isDirectory()) { - this.createUserConfig() + // + // fs.statSync will throw an exception if + // any directory along the path doesn't exist. + // Solution: use try catch. + try { + var fi = fs.statSync(userConfigPath); + if (!fi.isDirectory()) { + this.createUserConfig(userConfigPath); + } + } + catch(error) { + this.createUserConfig(userConfigPath); } - return userConfigPath; }, From cb57908e0d0b2c2c502c362e45f92a53ca0aa310 Mon Sep 17 00:00:00 2001 From: Jack Mordaunt Date: Wed, 3 Jan 2018 15:21:50 +0800 Subject: [PATCH 2/4] Refactored for clarity. --- app/public/js/common/configLocation.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js index b9e385c7..91e18f21 100644 --- a/app/public/js/common/configLocation.js +++ b/app/public/js/common/configLocation.js @@ -27,7 +27,6 @@ const configuration = { userConfigPath = `${userHome}/.config/Soundnode`; } - /** Linux platforms - XDG Standard */ if (process.platform === 'linux') { userConfigPath = `${userHome}/.config/Soundnode`; @@ -38,20 +37,16 @@ const configuration = { userConfigPath = `${userHome}/Library/Preferences/Soundnode`; } - // Guard to assert type of string. - if (typeof userConfigPath !== "string") { - throw `Could not set userConfigPath for this OS ${process.platform}` + /** Unsupported platform */ + if (userConfigPath === null) { + throw `could not set config path for this OS ${process.platform}` } // create user config in path // if there is no userConfig path - // - // fs.statSync will throw an exception if - // any directory along the path doesn't exist. - // Solution: use try catch. try { - var fi = fs.statSync(userConfigPath); - if (!fi.isDirectory()) { + const configFileInfo = fs.statSync(userConfigPath); + if (!configFileInfo.isDirectory()) { this.createUserConfig(userConfigPath); } } From 626d4419b10e13708514d53a5d8860eca4042385 Mon Sep 17 00:00:00 2001 From: Jack Mordaunt Date: Wed, 3 Jan 2018 15:29:25 +0800 Subject: [PATCH 3/4] Use method in favour of commented block. --- app/public/js/common/configLocation.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js index 91e18f21..65a8f280 100644 --- a/app/public/js/common/configLocation.js +++ b/app/public/js/common/configLocation.js @@ -14,6 +14,18 @@ const configuration = { }); }, + createIfNotExist(path) { + try { + const pathInfo = fs.statSync(path); + if (!pathInfo.isDirectory()) { + this.createUserConfig(path); + } + } + catch(error) { + this.createUserConfig(path); + } + }, + /** * Get the configuration folder location * @@ -42,17 +54,8 @@ const configuration = { throw `could not set config path for this OS ${process.platform}` } - // create user config in path - // if there is no userConfig path - try { - const configFileInfo = fs.statSync(userConfigPath); - if (!configFileInfo.isDirectory()) { - this.createUserConfig(userConfigPath); - } - } - catch(error) { - this.createUserConfig(userConfigPath); - } + createIfNotExist(userConfigPath) + return userConfigPath; }, From cf0ae2ed1964010b2ecb1a8141fdf797e2b1ace0 Mon Sep 17 00:00:00 2001 From: Jack Mordaunt Date: Wed, 3 Jan 2018 15:31:46 +0800 Subject: [PATCH 4/4] Typo fix. --- app/public/js/common/configLocation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js index 65a8f280..d9a7cfb1 100644 --- a/app/public/js/common/configLocation.js +++ b/app/public/js/common/configLocation.js @@ -54,7 +54,7 @@ const configuration = { throw `could not set config path for this OS ${process.platform}` } - createIfNotExist(userConfigPath) + this.createIfNotExist(userConfigPath) return userConfigPath; },