-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Fix #3412: Cryptic error message when addMenuItem() is passed bad args #3611
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,23 +91,32 @@ define(function (require, exports, module) { | |
|
||
|
||
/** | ||
* Insertion position constants | ||
* Used by addMenu(), addMenuItem(), and addSubMenu() to | ||
* specify the relative position of a newly created menu object | ||
* @enum {string} | ||
*/ | ||
var BEFORE = "before"; | ||
var AFTER = "after"; | ||
var FIRST = "first"; | ||
var LAST = "last"; | ||
var FIRST_IN_SECTION = "firstInSection"; | ||
var LAST_IN_SECTION = "lastInSection"; | ||
|
||
/** | ||
* Other constants | ||
*/ | ||
var DIVIDER = "---"; | ||
* Insertion position constants | ||
* Used by addMenu(), addMenuItem(), and addSubMenu() to | ||
* specify the relative position of a newly created menu object | ||
* @enum {string} | ||
*/ | ||
var BEFORE = "before", | ||
AFTER = "after", | ||
FIRST = "first", | ||
LAST = "last", | ||
FIRST_IN_SECTION = "firstInSection", | ||
LAST_IN_SECTION = "lastInSection"; | ||
|
||
/** | ||
* Other constants | ||
*/ | ||
var DIVIDER = "---"; | ||
|
||
/** | ||
* Error Codes from Brackets Shell | ||
* @enum {number} | ||
*/ | ||
var NO_ERROR = 0, | ||
ERR_UNKNOWN = 1, | ||
ERR_INVALID_PARAMS = 2, | ||
ERR_NOT_FOUND = 3; | ||
|
||
/** | ||
* Maps menuID's to Menu objects | ||
* @type {Object.<string, Menu>} | ||
|
@@ -462,7 +471,8 @@ define(function (require, exports, module) { | |
* @return {MenuItem} the newly created MenuItem | ||
*/ | ||
Menu.prototype.addMenuItem = function (command, keyBindings, position, relativeID) { | ||
var id, | ||
var menuID = this.id, | ||
id, | ||
$menuItem, | ||
$link, | ||
menuItem, | ||
|
@@ -547,8 +557,17 @@ define(function (require, exports, module) { | |
} | ||
|
||
brackets.app.addMenuItem(this.id, name, commandID, bindingStr, displayStr, position, relativeID, function (err) { | ||
if (err) { | ||
console.error("addMenuItem() -- error: " + err + " when adding command: " + commandID); | ||
switch (err) { | ||
case NO_ERROR: | ||
break; | ||
case ERR_INVALID_PARAMS: | ||
console.error("addMenuItem(): Invalid Parameters when adding the command " + commandID); | ||
break; | ||
case ERR_NOT_FOUND: | ||
console.error("_getRelativeMenuItem(): MenuItem with Command id " + relativeID + " not found in the Menu " + menuID); | ||
break; | ||
default: | ||
console.error("addMenuItem(); Unknown Error (" + err + ") when adding the command " + commandID); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also need a |
||
}); | ||
menuItem.isNative = true; | ||
|
@@ -800,15 +819,26 @@ define(function (require, exports, module) { | |
|
||
if (!_isHTMLMenu(id)) { | ||
brackets.app.addMenu(name, id, position, relativeID, function (err) { | ||
if (err) { | ||
console.error("addMenu() -- error: " + err + " when adding menu with ID: " + id); | ||
} else { | ||
switch (err) { | ||
case NO_ERROR: | ||
// Make sure name is up to date | ||
brackets.app.setMenuTitle(id, name, function (err) { | ||
if (err) { | ||
console.error("setMenuTitle() -- error: " + err); | ||
} | ||
}); | ||
break; | ||
case ERR_UNKNOWN: | ||
console.error("addMenu(): Unknown Error when adding the menu " + id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found this error in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be a catchall value passed from brackets-shell, so this is ok. This
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also need a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one does have a NO_ERROR case that does what the else branch did previously. |
||
break; | ||
case ERR_INVALID_PARAMS: | ||
console.error("addMenu(): Invalid Parameters when adding the menu " + id); | ||
break; | ||
case ERR_NOT_FOUND: | ||
console.error("addMenu(): Menu with command " + relativeID + " could not be found when adding the menu " + id); | ||
break; | ||
default: | ||
console.error("addMenu(): Unknown Error (" + err + ") when adding the menu " + id); | ||
} | ||
}); | ||
return menu; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
this.id
seems inconsistent in this function. It's stored inmenuID
which is only used in 1 place, and there are 3 other references tothis.id
. I think you can get rid of this var and just usethis.id
everywhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really. I added it to be able to access the menu ID in the
brckets.add.addMenuItem
callback function, since using this there will not refer to the Menu instance. Since I was going to use it once I saved the id there directly instead of doing something likethat = this
orself = this
and then usingthat.id
in the callback. But if this is a better solution I'll change it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that makes sense. Would it make sense to change all other
this.id
references tomenuID
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the other reference don't need to be changed since those can use the this object to access the menu id, but they could be changed if required.