Skip to content

Commit

Permalink
Error management
Browse files Browse the repository at this point in the history
API returns errors when due, and they are properly shown in the
interface.
  • Loading branch information
RedGlow committed Aug 3, 2015
1 parent e6937ca commit 059e617
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 27 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = function(grunt) {
'node_modules/angular/angular.min.js',
'node_modules/angular-animate/angular-animate.min.js',
'node_modules/ngstorage/ngStorage.min.js',
'node_modules/angular-ui-bootstrap/ui-bootstrap-tpls.min.js',
'node_modules/bootstrap/js/modal.js',
'src/program/*.js'
]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"devDependencies": {
"angular": "^1.4.3",
"angular-animate": "^1.4.3",
"angular-ui-bootstrap": "^0.13.2",
"bootstrap": "^3.3.5",
"grunt": "^0.4.5",
"grunt-cli": "^0.1.13",
Expand Down
33 changes: 33 additions & 0 deletions src/origin-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<script src="../node_modules/angular/angular.min.js"></script>
<script src="../node_modules/angular-animate/angular-animate.min.js"></script>
<script src="../node_modules/ngstorage/ngStorage.min.js"></script>
<script src="../node_modules/angular-ui-bootstrap/ui-bootstrap-tpls.min.js"></script>
<script src="../node_modules/bootstrap/js/modal.js"></script>
<script src="program/recipe_companion.js"></script>
<script src="program/bank.js"></script>
Expand Down Expand Up @@ -156,6 +157,38 @@
</div>
</script>

<!-- ERROR DIALOG TEMPLATE -->
<script type="text/ng-template" id="error-dialog.html">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close" ng-click="$dismiss()"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Error</h4>
</div>
<div class="modal-body">
<p>An error was encountered.</p>
<p>Try reloading the page or launch the request again.</p>
<p>If the problem persists, please <a href="https://github.com/RedGlow/TheLegendaryQuest/issues/new"
target="_blank">open
a bug report</a> and possibly <a href="mailto:mattia.belletti@gmail.com?subject=API key for bug #&lt;INSERT BUG NUMBER&gt;">send me an email with your API
key and a reference to the bug you opened</a> at <em>mattia.belletti@gmail.com</em>.</p>
<p>
<button class="btn btn-default" role="button" ng-click="toggleDetails()">
<span class="bug-button glyphicon" ng-class="{'glyphicon-folder-close': openedDetails, 'glyphicon-folder-open': !openedDetails}"></span>
<span ng-if="!openedDetails">Open details</span>
<span ng-if="openedDetails">Close details</span>
</button>
</p>
<pre ng-if="openedDetails">
Status: {{ error.status }}
Status text: {{ error.statusText }}
Config: {{ error.config | json }}
Data: {{ error.data | json }}
</pre>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="$dismiss()">Close</button>
</div>
</script>

<div class="running-requests" ng-class="{'invisible': runningRequests() <= 0}">
<p class="content">
<img src="img/loading.gif">
Expand Down
54 changes: 34 additions & 20 deletions src/program/gw2api.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ angular.module('supplyCrateApp.gw2api', [
numRunningRequests--;
}
delete request.queued[id];
for(var k = 0; k < ids.length; k++) {
for(var k = 0; k < queueIds.length; k++) {
queueId = queueIds[k];
if(queueId == id) {
queueIds.splice(k, 1);
break;
}
}
queueIds.splice(k, 1);
}
for(i = 0; i < queueIds.length; i++) {
queueId = queueIds[i];
Expand All @@ -154,24 +154,38 @@ angular.module('supplyCrateApp.gw2api', [
delete request.queued[queueId];
}
}, function(err) {
// all values are invalid
var now = Now.value();
for(var i = 0; i < queueIds.length; i++) {
var queueId = queueIds[i];
var queueRow = request.queued[queueId];
var returnValue = {
"text": "all ids provided are invalid"
};
request.cache[queueId] = {
timestamp: now,
value: returnValue,
isRejection: true
};
for(var j = 0; j < queueRow.length; j++) {
queueRow[j].reject(returnValue);
numRunningRequests--;
}
delete request.queued[queueId];
if(!!err.data && !!err.data.text && err.data.text == "all ids provided are invalid") {
// all values are invalid
var now = Now.value();
for(var i = 0; i < queueIds.length; i++) {
var queueId = queueIds[i];
var queueRow = request.queued[queueId];
var returnValue = {
"text": "all ids provided are invalid"
};
request.cache[queueId] = {
timestamp: now,
value: returnValue,
isRejection: true
};
for(var j = 0; j < queueRow.length; j++) {
queueRow[j].reject(returnValue);
numRunningRequests--;
}
delete request.queued[queueId];
}
} else {
// this is another kind of error (HTTP level, 500, ...):
// return the same error, but without caching it
for(var i = 0; i < queueIds.length; i++) {
var queueId = queueIds[i];
var queueRow = request.queued[queueId];
for(var j = 0; j < queueRow.length; j++) {
queueRow[j].reject(err);
numRunningRequests--;
}
delete request.queued[queueId];
}
}
});
}
Expand Down
37 changes: 31 additions & 6 deletions src/program/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
angular.module('legendarySearch.main', [
'ngStorage',
'ui.bootstrap',
'legendarySearch',
'supplyCrateApp.gw2api',
'legendarySearch.bank',
Expand All @@ -12,8 +13,23 @@ angular.module('legendarySearch.main', [
])

.controller('Main', [
"$scope", "$q", "$localStorage", "GW2API", "Bank", "RecursiveRecipeComputer", "RunningRequests", "RecipeCompanion",
function($scope, $q, $localStorage, GW2API, Bank, RecursiveRecipeComputer, RunningRequests, RecipeCompanion) {
"$scope", "$q", "$localStorage", "$modal", "GW2API", "Bank", "RecursiveRecipeComputer", "RunningRequests", "RecipeCompanion",
function($scope, $q, $localStorage, $modal, GW2API, Bank, RecursiveRecipeComputer, RunningRequests, RecipeCompanion) {
// error function
function errorFunction(error) {
return $modal.open({
templateUrl: 'error-dialog.html',
controller: 'ErrorDialogController',
resolve: {
error: function() { return error; }
}
}).result.then(function() {
return $q.reject(error);
}, function() {
return $q.reject(error);
});
}

// initialize legendary list
var availableLegendariesIds = RecipeCompanion.getLegendaryIds();
$q.all(jQuery.map(availableLegendariesIds, function(legendaryId) {
Expand All @@ -25,7 +41,7 @@ angular.module('legendarySearch.main', [
return l1.name.localeCompare(l2.name);
});
$scope.availableLegendaries = availableLegendaries;
});
}, errorFunction);
$scope.selectedLegendary = null;

// initialize TP management
Expand Down Expand Up @@ -64,9 +80,7 @@ angular.module('legendarySearch.main', [
.then(function(data) {
console.debug(data);
$scope.costTree = data;
}, function(error) {
alert("Cost tree error:", error);
});
}, errorFunction);
}
$scope.$watch('bankContent', reloadTree);
$scope.$watch('selectedLegendary', reloadTree);
Expand All @@ -77,4 +91,15 @@ angular.module('legendarySearch.main', [
}
])

.controller('ErrorDialogController', [
"$scope", "error",
function($scope, error) {
$scope.error = error;
$scope.openedDetails = false;
$scope.toggleDetails = function() {
$scope.openedDetails = !$scope.openedDetails;
}
}
])

;
8 changes: 7 additions & 1 deletion src/program/recursive_recipe_computer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ angular.module('legendarySearch.recursiveRecipeComputer', [
var tradingPostCostPromise = GW2API
.getListing(itemId)
.then(function(result) { return result; },
function(error) { return {id: itemId, buys: [], sells: []}; });
function(error) {
if(!!error.text && error.text == "all ids provided are invalid") {
return {id: itemId, buys: [], sells: []};
} else {
return $q.reject(error);
}
});
// run them
return $q.all([recipePromise, tradingPostCostPromise]).then(function(results) {
var recipeResult = results[0],
Expand Down
6 changes: 6 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,10 @@ a.expansion-arrow {

.results-row {
overflow-x: auto;
}

/* ERROR DIALOG */

.bug-button {
margin-right: 0.5em;
}

0 comments on commit 059e617

Please sign in to comment.