From 1f5272415c7f5cdfcc0b5a3358cc352037b12c0b Mon Sep 17 00:00:00 2001 From: juliandescottes Date: Mon, 13 Apr 2015 14:33:34 +0200 Subject: [PATCH] #261 replaced throw by console.error for recoverable errors --- .../controller/settings/ImportController.js | 4 ++-- src/js/model/Layer.js | 8 +++----- src/js/selection/SelectionManager.js | 12 +++-------- src/js/service/HistoryService.js | 20 +++++++++++-------- src/js/service/LocalStorageService.js | 1 - .../service/palette/PaletteImportService.js | 2 +- src/js/utils/UserSettings.js | 2 +- 7 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/js/controller/settings/ImportController.js b/src/js/controller/settings/ImportController.js index 1a2df04..d2c3ea4 100644 --- a/src/js/controller/settings/ImportController.js +++ b/src/js/controller/settings/ImportController.js @@ -15,7 +15,7 @@ this.addEventListener('.browse-local-button', 'click', this.onBrowseLocalClick_); this.addEventListener('.file-input-button', 'click', this.onFileInputClick_); - + // different handlers, depending on the Environment if (pskl.utils.Environment.detectNodeWebkit()) { this.addEventListener('.open-piskel-button', 'click', this.openPiskelDesktop_); @@ -98,7 +98,7 @@ this.closeDrawer_(); } else { this.closeDrawer_(); - throw 'File is not an image : ' + file.type; + console.error('File is not an image : ' + file.type); } } }; diff --git a/src/js/model/Layer.js b/src/js/model/Layer.js index 206aa56..ea46061 100644 --- a/src/js/model/Layer.js +++ b/src/js/model/Layer.js @@ -56,7 +56,7 @@ if (this.frames[index]) { this.frames.splice(index, 1); } else { - throw 'Invalid index in removeFrameAt : ' + index + ' (size : ' + this.size() + ')'; + console.error('Invalid index in removeFrameAt : %s (size : %s)', index, this.size()); } }; @@ -72,9 +72,7 @@ this.frames[toIndex] = fromFrame; this.frames[fromIndex] = toFrame; } else { - console.log('frames', this.frames); - console.log('fromIndex', fromIndex, 'toIndex', toIndex); - throw 'Frame not found in moveFrameAt'; + console.error('Frame not found in moveFrameAt (from %s, to %s)', fromIndex, toIndex); } }; @@ -89,7 +87,7 @@ var clone = frame.clone(); this.addFrameAt(clone, index); } else { - throw 'Frame not found in duplicateFrameAt'; + console.error('Frame not found in duplicateFrameAt (at %s)', index); } }; diff --git a/src/js/selection/SelectionManager.js b/src/js/selection/SelectionManager.js index e6a9021..b7b4034 100644 --- a/src/js/selection/SelectionManager.js +++ b/src/js/selection/SelectionManager.js @@ -85,9 +85,6 @@ this.currentSelection.fillSelectionFromFrame(this.piskelController.getCurrentFrame()); this.erase(); } - else { - throw "Bad state for CUT callback in SelectionManager"; - } }; ns.SelectionManager.prototype.paste = function() { @@ -128,8 +125,6 @@ ns.SelectionManager.prototype.copy = function() { if(this.currentSelection && this.piskelController.getCurrentFrame()) { this.currentSelection.fillSelectionFromFrame(this.piskelController.getCurrentFrame()); - } else { - throw "Bad state for CUT callback in SelectionManager"; } }; @@ -140,7 +135,7 @@ if(selection) { this.currentSelection = selection; } else { - throw "No selection set in SelectionManager"; + console.error("No selection provided to SelectionManager"); } }; @@ -150,9 +145,8 @@ ns.SelectionManager.prototype.onSelectionMoved_ = function(evt, colDiff, rowDiff) { if(this.currentSelection) { this.currentSelection.move(colDiff, rowDiff); - } - else { - throw "Bad state: No currentSelection set when trying to move it in SelectionManager"; + } else { + console.error("Bad state: No currentSelection set when trying to move it in SelectionManager"); } }; })(); diff --git a/src/js/service/HistoryService.js b/src/js/service/HistoryService.js index b974bb0..de20a0f 100644 --- a/src/js/service/HistoryService.js +++ b/src/js/service/HistoryService.js @@ -92,19 +92,23 @@ var onPiskelLoadedCb = this.onPiskelLoaded_.bind(this, index, snapshotIndex); this.deserializer.deserialize(serializedPiskel, onPiskelLoadedCb); } - } catch (e) { - window.console.error("[CRITICAL ERROR] : Unable to load a history state."); - if (typeof e === "string") { - window.console.error(e); - } else { - window.console.error(e.message); - window.console.error(e.stack); - } + } catch (error) { + console.error("[CRITICAL ERROR] : Unable to load a history state."); + this.logError_(error); this.stateQueue = []; this.currentIndex = -1; } }; + ns.HistoryService.prototype.logError_ = function (error) { + if (typeof error === "string") { + console.error(error); + } else { + console.error(error.message); + console.error(error.stack); + } + }; + ns.HistoryService.prototype.getSnapshotFromState_ = function (stateIndex) { var state = this.stateQueue[stateIndex]; var piskelSnapshot = state.piskel; diff --git a/src/js/service/LocalStorageService.js b/src/js/service/LocalStorageService.js index 8706354..4ac0956 100644 --- a/src/js/service/LocalStorageService.js +++ b/src/js/service/LocalStorageService.js @@ -2,7 +2,6 @@ var ns = $.namespace("pskl.service"); ns.LocalStorageService = function (piskelController) { - if(piskelController === undefined) { throw "Bad LocalStorageService initialization: "; } diff --git a/src/js/service/palette/PaletteImportService.js b/src/js/service/palette/PaletteImportService.js index 0b954c5..e00371a 100644 --- a/src/js/service/palette/PaletteImportService.js +++ b/src/js/service/palette/PaletteImportService.js @@ -15,7 +15,7 @@ if (reader) { reader.read(); } else { - throw 'Could not find reader for file : ' + file.name; + console.error('Could not find reader for file : %s', file.name); } }; diff --git a/src/js/utils/UserSettings.js b/src/js/utils/UserSettings.js index ed24ab7..8e7042c 100644 --- a/src/js/utils/UserSettings.js +++ b/src/js/utils/UserSettings.js @@ -87,7 +87,7 @@ checkKeyValidity_ : function(key) { if(!(key in this.KEY_TO_DEFAULT_VALUE_MAP_)) { // TODO(grosbouddha): Define error catching strategy and throw exception from here. - console.log("UserSettings key <"+ key +"> not find in supported keys."); + console.error("UserSettings key <"+ key +"> not found in supported keys."); } } };