From c236325c1a5f2753cbc08be834d734174eda0029 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 12 Mar 2013 16:47:26 -0700 Subject: [PATCH] Log errors (instead of crashing) when the config file cannot be parsed Also, config won't overwrite changes to config.cson when the file can not be parsed. Closes #401 --- spec/app/config-spec.coffee | 19 +++++++++++++++++++ src/app/config.coffee | 11 +++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spec/app/config-spec.coffee b/spec/app/config-spec.coffee index 7062c4042..39aae22fe 100644 --- a/spec/app/config-spec.coffee +++ b/spec/app/config-spec.coffee @@ -1,3 +1,4 @@ +Config = require 'config' fs = require 'fs' describe "Config", -> @@ -133,3 +134,21 @@ describe "Config", -> expect(fs.isFile(fs.join(config.configDirPath, 'themes/atom-light-ui/package.cson'))).toBeTruthy() expect(fs.isFile(fs.join(config.configDirPath, 'themes/atom-dark-syntax.css'))).toBeTruthy() expect(fs.isFile(fs.join(config.configDirPath, 'themes/atom-light-syntax.css'))).toBeTruthy() + + describe "when the config file is not parseable", -> + beforeEach -> + config.configDirPath = '/tmp/dot-atom-dir' + config.configFilePath = fs.join(config.configDirPath, "config.cson") + expect(fs.exists(config.configDirPath)).toBeFalsy() + + afterEach -> + fs.remove('/tmp/dot-atom-dir') if fs.exists('/tmp/dot-atom-dir') + + it "logs an error to the console and does not overwrite the config file", -> + config.save.reset() + spyOn(console, 'error') + fs.write(config.configFilePath, "{{{{{") + config.loadUserConfig() + config.set("hair", "blonde") # trigger a save + expect(console.error).toHaveBeenCalled() + expect(config.save).not.toHaveBeenCalled() \ No newline at end of file diff --git a/src/app/config.coffee b/src/app/config.coffee index 7c22c362e..dc79df9ee 100644 --- a/src/app/config.coffee +++ b/src/app/config.coffee @@ -20,6 +20,7 @@ class Config userPackagesDirPath: userPackagesDirPath defaultSettings: null settings: null + configFileHasErrors: null constructor: -> @defaultSettings = @@ -55,8 +56,13 @@ class Config loadUserConfig: -> if fs.exists(@configFilePath) - userConfig = fs.readObject(@configFilePath) - _.extend(@settings, userConfig) + try + userConfig = fs.readObject(@configFilePath) + _.extend(@settings, userConfig) + catch e + @configFileHasErrors = true + console.error "Failed to load user config '#{@configFilePath}'", e.message + console.error e.stack get: (keyPath) -> _.valueForKeyPath(@settings, keyPath) ? @@ -92,6 +98,7 @@ class Config subscription update: -> + return if @configFileHasErrors @save() @trigger 'updated'