Log warning when ~/.atom/keymap.cson can't be parsed

Previously an uncaught error would be thrown and the file
would no longer be watched
This commit is contained in:
Kevin Sawicki
2014-02-20 14:30:29 -08:00
parent 090f737647
commit 4c65d711b0
2 changed files with 24 additions and 1 deletions

View File

@@ -400,6 +400,22 @@ describe "Keymap", ->
keyBinding = keymap.keyBindingsForKeystroke('ctrl-l')[0]
expect(keyBinding).toBeUndefined()
it "logs a warning when it can't be parsed", ->
keymapFilePath = path.join(configDirPath, "keymap.json")
fs.writeFileSync(keymapFilePath, '')
keymap.loadUserKeymap()
spyOn(keymap, 'loadUserKeymap').andCallThrough()
fs.writeFileSync(keymapFilePath, '}{')
spyOn(console, 'warn')
waitsFor ->
keymap.loadUserKeymap.callCount > 0
runs ->
expect(console.warn.callCount).toBe 1
expect(console.warn.argsForCall[0][0].length).toBeGreaterThan 0
describe "when adding a binding with an invalid selector", ->
it "logs a warning and does not add it", ->
spyOn(console, 'warn')

View File

@@ -135,9 +135,16 @@ class Keymap
userKeymapPath = @getUserKeymapPath()
if fs.isFileSync(userKeymapPath)
@userKeymapPath = userKeymapPath
@load(userKeymapPath)
@userKeymapFile = new File(userKeymapPath)
@userKeymapFile.on 'contents-changed moved removed', => @loadUserKeymap()
@add(@userKeymapPath, @readUserKeymap())
readUserKeymap: ->
try
CSON.readFileSync(@userKeymapPath) ? {}
catch error
console.warn("Failed to load your keymap file: #{@userKeymapPath}", error.stack ? error)
{}
loadDirectory: (directoryPath) ->
platforms = ['darwin', 'freebsd', 'linux', 'sunos', 'win32']