From ebb8ff96f31b1b24e714971df932a6e0f809a0bb Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 19 Oct 2010 19:57:45 +0200 Subject: [PATCH] improved error handling --- frontend/javascripts/frontend.js | 49 ++++++++++++++++++++++++++++---- frontend/javascripts/helper.js | 5 +++- frontend/javascripts/uuid.js | 8 +++--- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/frontend/javascripts/frontend.js b/frontend/javascripts/frontend.js index ce34904..af89ceb 100644 --- a/frontend/javascripts/frontend.js +++ b/frontend/javascripts/frontend.js @@ -107,8 +107,8 @@ vz.initDialogs = function() { $('#addUUID').dialog('close'); vz.entities.loadDetails(); } - catch (e) { - alert(e); // TODO show error + catch (exception) { + vz.exceptionDialog(exception); } }); }; @@ -320,9 +320,16 @@ vz.entities.loadData = function() { $('#plot').html('
loading...

Loading...

'); vz.entities.each(function(entity, parent) { if (entity.active && entity.type != 'group') { - vz.load('data', entity.uuid, { from: Math.floor(vz.options.plot.xaxis.min), to: Math.ceil(vz.options.plot.xaxis.max), tuples: vz.options.tuples }, waitAsync(function(json) { - entity.data = json.data; - }, vz.drawPlot, 'data')); + vz.load('data', entity.uuid, + { + from: Math.floor(vz.options.plot.xaxis.min), + to: Math.ceil(vz.options.plot.xaxis.max), + tuples: vz.options.tuples + }, + waitAsync(function(json) { + entity.data = json.data; + }, vz.drawPlot, 'data') + ); } }); }; @@ -357,7 +364,37 @@ vz.load = function(context, identifier, data, success) { data: data, error: function(xhr) { json = JSON.parse(xhr.responseText); - alert(xhr.status + ': ' + xhr.statusText + '\n' + json.exception.message); + vz.errorDialog(xhr.statusText, json.exception.message, xhr.status); } }); }; + +/* + * Error & Exception handling + */ + +vz.errorDialog = function(error, description, code) { + if (typeof code != undefined) { + error = code + ': ' + error; + } + + $('
') + .addClass('error') + .append($('').text(description)) + .dialog({ + title: error, + width: 450 + }); +}; + +vz.exceptionDialog = function(exception) { + vz.errorDialog(exception.type, exception.message, exception.code); +}; + +var Exception(type, message, code) { + return { + type: type, + message: message, + code: code + }; +} diff --git a/frontend/javascripts/helper.js b/frontend/javascripts/helper.js index b20f706..17d4566 100644 --- a/frontend/javascripts/helper.js +++ b/frontend/javascripts/helper.js @@ -24,6 +24,9 @@ * volkszaehler.org. If not, see . */ +/** + * Helper function to wait for multiple ajax requests to complete + */ function waitAsync(callback, finished, identifier) { if (!waitAsync.counter) { waitAsync.counter = new Array(); } if (!waitAsync.counter[identifier]) { waitAsync.counter[identifier] = 0; } @@ -63,4 +66,4 @@ Array.prototype.contains = function(n) { Array.prototype.clear = function() { this.length = 0; -} \ No newline at end of file +} diff --git a/frontend/javascripts/uuid.js b/frontend/javascripts/uuid.js index c2b11eb..733ce4f 100644 --- a/frontend/javascripts/uuid.js +++ b/frontend/javascripts/uuid.js @@ -45,11 +45,11 @@ vz.uuids.add = function(uuid) { $.setCookie('vz_uuids', JSON.stringify(vz.uuids)); } else { - throw 'UUID already added'; + throw new Exception('UUIDException', 'UUID already added'); } } else { - throw 'Invalid UUID'; + throw new Exception('UUIDException', 'Invalid UUID'); } }; @@ -63,7 +63,7 @@ vz.uuids.remove = function(uuid) { $.setCookie('vz_uuids', JSON.stringify(vz.uuids)); } else { - throw 'UUID unkown: ' + uuid; + throw new Exception('UUIDException', 'UUID unkown: ' + uuid); } }; @@ -72,4 +72,4 @@ vz.uuids.remove = function(uuid) { */ vz.uuids.validate = function(uuid) { return uuid.match(/^[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}$/); -}; \ No newline at end of file +};