Show notification for invalid context menu selector

This commit is contained in:
Kevin Sawicki
2015-03-03 16:51:38 -08:00
parent a2d9ba2d2e
commit 59c3dea77b
4 changed files with 49 additions and 15 deletions

View File

@@ -0,0 +1,10 @@
{
"context-menu": {
"<>": [
{
"label": "Hello",
"command:": "world"
}
]
}
}

View File

@@ -0,0 +1,4 @@
{
"name": "package-with-invalid-context-menu",
"version": "1.0.0"
}

View File

@@ -2,7 +2,7 @@
Package = require '../src/package'
{Disposable} = require 'atom'
fdescribe "PackageManager", ->
describe "PackageManager", ->
workspaceElement = null
beforeEach ->
@@ -212,13 +212,20 @@ fdescribe "PackageManager", ->
runs ->
expect(mainModule.activate.callCount).toBe 1
it "logs a warning when the activation commands are invalid", ->
it "adds a notification when the activation commands are invalid", ->
addErrorHandler = jasmine.createSpy()
atom.notifications.onDidAddNotification(addErrorHandler)
expect(-> atom.packages.activatePackage('package-with-invalid-activation-commands')).not.toThrow()
expect(addErrorHandler.callCount).toBe 1
expect(addErrorHandler.argsForCall[0][0].message).toContain("Failed to activate the package-with-invalid-activation-commands package")
it "adds a notification when the context menu is invalid", ->
addErrorHandler = jasmine.createSpy()
atom.notifications.onDidAddNotification(addErrorHandler)
expect(-> atom.packages.activatePackage('package-with-invalid-context-menu')).not.toThrow()
expect(addErrorHandler.callCount).toBe 1
expect(addErrorHandler.argsForCall[0][0].message).toContain("Failed to activate the package-with-invalid-context-menu package")
describe "when the package has no main module", ->
it "does not throw an exception", ->
spyOn(console, "error")

View File

@@ -143,18 +143,14 @@ class Package
unless @activationDeferred?
@activationDeferred = Q.defer()
@measure 'activateTime', =>
@activateResources()
if @hasActivationCommands()
try
try
@activateResources()
if @hasActivationCommands()
@subscribeToActivationCommands()
catch error
if error.code is 'EBADSELECTOR'
metadataPath = path.join(@path, 'package.json')
error.message += " in #{metadataPath}"
error.stack += "\n at #{metadataPath}:1:1"
@handleError("Failed to activate the #{@name} package", error)
else
@activateNow()
else
@activateNow()
catch error
@handleError("Failed to activate the #{@name} package", error)
Q.all([@grammarsPromise, @settingsPromise, @activationDeferred.promise])
@@ -206,7 +202,16 @@ class Package
activateResources: ->
@activationDisposables = new CompositeDisposable
@activationDisposables.add(atom.keymaps.add(keymapPath, map)) for [keymapPath, map] in @keymaps
@activationDisposables.add(atom.contextMenu.add(map['context-menu'])) for [menuPath, map] in @menus when map['context-menu']?
for [menuPath, map] in @menus when map['context-menu']?
try
@activationDisposables.add(atom.contextMenu.add(map['context-menu']))
catch error
if error.code is 'EBADSELECTOR'
error.message += " in #{menuPath}"
error.stack += "\n at #{menuPath}:1:1"
throw error
@activationDisposables.add(atom.menu.add(map['menu'])) for [menuPath, map] in @menus when map['menu']?
unless @grammarsActivated
@@ -417,7 +422,15 @@ class Package
do (selector, command) =>
# Add dummy command so it appears in menu.
# The real command will be registered on package activation
@activationCommandSubscriptions.add atom.commands.add selector, command, ->
try
@activationCommandSubscriptions.add atom.commands.add selector, command, ->
catch error
if error.code is 'EBADSELECTOR'
metadataPath = path.join(@path, 'package.json')
error.message += " in #{metadataPath}"
error.stack += "\n at #{metadataPath}:1:1"
throw error
@activationCommandSubscriptions.add atom.commands.onWillDispatch (event) =>
return unless event.type is command
currentTarget = event.target