improved error handling

This commit is contained in:
Steffen Vogel 2010-10-19 19:57:45 +02:00
parent e5a3b6aa3e
commit ebb8ff96f3
3 changed files with 51 additions and 11 deletions

View file

@ -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('<div class="plotcenter"><img src="images/loading.gif" alt="loading..." /><p>Loading...</p></div>');
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;
}
$('<div>')
.addClass('error')
.append($('<span>').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
};
}

View file

@ -24,6 +24,9 @@
* volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* 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;
}
}

View file

@ -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}$/);
};
};