From 4895a7645b43aa205dcf7b4add53d2fec9458075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Malbr=C3=A1n?= Date: Wed, 24 Apr 2013 21:04:47 -0300 Subject: [PATCH 1/2] Better error messages for AddMenu and AddMenuItem --- src/command/Menus.js | 68 ++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/src/command/Menus.js b/src/command/Menus.js index 455aecb6151..1e19aa7812b 100644 --- a/src/command/Menus.js +++ b/src/command/Menus.js @@ -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"; + var AFTER = "after"; + var FIRST = "first"; + var LAST = "last"; + var FIRST_IN_SECTION = "firstInSection"; + var LAST_IN_SECTION = "lastInSection"; + /** + * Other constants + */ + var DIVIDER = "---"; + + /** + * Error Codes from Brackets Shell + * @enum {number} + */ + var NO_ERROR = 0; + var ERR_UNKNOWN = 1; + var ERR_INVALID_PARAMS = 2; + var ERR_NOT_FOUND = 3; + /** * Maps menuID's to Menu objects * @type {Object.} @@ -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,13 @@ 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 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; } }); menuItem.isNative = true; @@ -800,15 +815,24 @@ 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 ERR_UNKNOWN: + console.error("addMenu(): Unknown Error when adding the menu " + id); + 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; + 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; } }); return menu; From d3abaa4668b19f4c1f464a3c4df2f3449fed3039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Malbr=C3=A1n?= Date: Fri, 3 May 2013 20:50:08 -0300 Subject: [PATCH 2/2] Added NO_ERROR and default cases. Merged some vars. --- src/command/Menus.js | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/command/Menus.js b/src/command/Menus.js index 1e19aa7812b..302535de2d0 100644 --- a/src/command/Menus.js +++ b/src/command/Menus.js @@ -96,12 +96,12 @@ define(function (require, exports, module) { * 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"; + var BEFORE = "before", + AFTER = "after", + FIRST = "first", + LAST = "last", + FIRST_IN_SECTION = "firstInSection", + LAST_IN_SECTION = "lastInSection"; /** * Other constants @@ -112,10 +112,10 @@ define(function (require, exports, module) { * Error Codes from Brackets Shell * @enum {number} */ - var NO_ERROR = 0; - var ERR_UNKNOWN = 1; - var ERR_INVALID_PARAMS = 2; - var ERR_NOT_FOUND = 3; + var NO_ERROR = 0, + ERR_UNKNOWN = 1, + ERR_INVALID_PARAMS = 2, + ERR_NOT_FOUND = 3; /** * Maps menuID's to Menu objects @@ -558,12 +558,16 @@ define(function (require, exports, module) { brackets.app.addMenuItem(this.id, name, commandID, bindingStr, displayStr, position, relativeID, function (err) { 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); } }); menuItem.isNative = true; @@ -816,6 +820,14 @@ define(function (require, exports, module) { if (!_isHTMLMenu(id)) { brackets.app.addMenu(name, id, position, relativeID, function (err) { 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); break; @@ -825,14 +837,8 @@ define(function (require, exports, module) { case ERR_NOT_FOUND: console.error("addMenu(): Menu with command " + relativeID + " could not be found when adding the menu " + id); break; - 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; + default: + console.error("addMenu(): Unknown Error (" + err + ") when adding the menu " + id); } }); return menu;