Browse Source

Улучшение обработки ошибок

Book Pauk 2 years ago
parent
commit
e865070f23
1 changed files with 21 additions and 17 deletions
  1. 21 17
      server/config/index.js

+ 21 - 17
server/config/index.js

@@ -83,23 +83,27 @@ class ConfigManager {
     }
 
     async load() {
-        if (!this.inited)
-            throw new Error('not inited');
-
-        if (await fs.pathExists(this.userConfigFile)) {
-            const data = JSON.parse(await fs.readFile(this.userConfigFile, 'utf8'));
-            const config = _.pick(data, propsToSave);
-            
-            this.config = config;
-
-            //сохраним конфиг, если не все атрибуты присутствуют в файле конфига
-            for (const prop of propsToSave)
-                if (!Object.prototype.hasOwnProperty.call(config, prop)) {
-                    await this.save();
-                    break;
-                }
-        } else {
-            await this.save();
+        try {
+            if (!this.inited)
+                throw new Error('not inited');
+
+            if (await fs.pathExists(this.userConfigFile)) {
+                const data = JSON.parse(await fs.readFile(this.userConfigFile, 'utf8'));
+                const config = _.pick(data, propsToSave);
+
+                this.config = config;
+
+                //сохраним конфиг, если не все атрибуты присутствуют в файле конфига
+                for (const prop of propsToSave)
+                    if (!Object.prototype.hasOwnProperty.call(config, prop)) {
+                        await this.save();
+                        break;
+                    }
+            } else {
+                await this.save();
+            }
+        } catch(e) {
+            throw new Error(`Error while loading "${this.userConfigFile}": ${e.message}`);
         }
     }