Added exception and error messages to egw_json

This commit is contained in:
Andreas Stöckel 2010-06-21 08:39:21 +00:00
parent 729c7cb362
commit 01826c38b2
2 changed files with 156 additions and 115 deletions

View File

@ -332,6 +332,17 @@ class egw_json_response
} }
} }
/**
* Displays an error message on the client
*/
public function error($msg)
{
if (is_string($msg))
{
$this->addGeneric('error', $msg);
}
}
/** /**
* Includes the given CSS file. Every url can only be included once. * Includes the given CSS file. Every url can only be included once.
* *

View File

@ -204,14 +204,24 @@ egw_json_request.prototype.alertFunc = function(_message, _details)
alert(_message); alert(_message);
} }
function _egw_json_debug_log(_msg) function _egw_json_debug_log(_msg, _e)
{ {
if (typeof console != "undefined" && typeof console.log != "undefined") if (typeof console != "undefined" && typeof console.log != "undefined")
{ {
console.log(_msg); console.log(_msg, _e);
} }
} }
/* Displays an json error message */
egw_json_request.prototype.jsonError = function(_msg, _e)
{
var msg = 'EGW JSON Error: '._msg;
//Log and show the error message
_egw_json_bebug_log(msg, _e);
this.alertHandler(msg);
}
/* Internal function which handles the response from the server */ /* Internal function which handles the response from the server */
egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRequest) egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRequest)
{ {
@ -220,132 +230,152 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
var hasResponse = false; var hasResponse = false;
for (var i = 0; i < data.response.length; i++) for (var i = 0; i < data.response.length; i++)
{ {
var res = data.response[i]; try
switch (data.response[i].type)
{ {
case 'alert': var res = data.response[i];
//Check whether all needed parameters have been passed and call the alertHandler function
if ((typeof res.data.message != 'undefined') && switch (data.response[i].type)
(typeof res.data.details != 'undefined')) {
{ case 'alert':
this.alertHandler( //Check whether all needed parameters have been passed and call the alertHandler function
res.data.message, if ((typeof res.data.message != 'undefined') &&
res.data.details) (typeof res.data.details != 'undefined'))
hasResponse = true; {
} this.alertHandler(
break; res.data.message,
case 'assign': res.data.details)
//Check whether all needed parameters have been passed and call the alertHandler function hasResponse = true;
if ((typeof res.data.id != 'undefined') && } else
(typeof res.data.key != 'undefined') && throw 'Invalid parameters';
(typeof res.data.value != 'undefined')) break;
{ case 'assign':
var obj = document.getElementById(res.data.id); //Check whether all needed parameters have been passed and call the alertHandler function
if (obj) if ((typeof res.data.id != 'undefined') &&
(typeof res.data.key != 'undefined') &&
(typeof res.data.value != 'undefined'))
{
var obj = document.getElementById(res.data.id);
if (obj)
{
obj[res.data.key] = res.data.value;
hasResponse = true;
}
} else
throw 'Invalid parameters';
break;
case 'data':
//Callback the caller in order to allow him to handle the data
if (this.callback)
{ {
obj[res.data.key] = res.data.value; this.callback.call(this.sender, res.data);
hasResponse = true; hasResponse = true;
} }
} break;
break; case 'script':
case 'data': if (typeof res.data == 'string')
//Callback the caller in order to allow him to handle the data
if (this.callback)
{
this.callback.call(this.sender, res.data);
hasResponse = true;
}
break;
case 'script':
if (typeof res.data == 'string')
{
try
{ {
var func = function() {eval(res.data);}; try
func.call(window); {
} var func = function() {eval(res.data);};
catch (e) func.call(window);
}
catch (e)
{
e.code = res.data;
_egw_json_debug_log(e);
}
hasResponse = true;
} else
throw 'Invalid parameters';
break;
case 'jquery':
if (typeof res.data.select == 'string' &&
typeof res.data.func == 'string')
{ {
e.code = res.data; try
_egw_json_debug_log(e); {
} var jQueryObject = $(res.data.select, this.context);
hasResponse = true; jQueryObject[res.data.func].apply(jQueryObject, res.data.parms);
} }
break; catch (e)
case 'jquery': {
if (typeof res.data.select == 'string' && _egw_json_debug_log(e);
typeof res.data.func == 'string') }
{ hasResponse = true;
try } else
throw 'Invalid parameters';
break;
case 'redirect':
if (typeof res.data.url == 'string' &&
typeof res.data.global == 'boolean')
{ {
var jQueryObject = $(res.data.select, this.context); //Special handling for framework reload
jQueryObject[res.data.func].apply(jQueryObject, res.data.parms); if (res.data.url.indexOf("?cd=10") > 0)
} res.data.global = true;
catch (e)
{
_egw_json_debug_log(e);
}
hasResponse = true;
}
break;
case 'redirect':
if (typeof res.data.url == 'string' &&
typeof res.data.global == 'boolean')
{
//Special handling for framework reload
if (res.data.url.indexOf("?cd=10") > 0)
res.data.global = true;
if (res.data.global) if (res.data.global)
{ {
egw_topWindow().location.href = res.data.url; egw_topWindow().location.href = res.data.url;
} }
else else
{ {
window.location.href = res.data.url; window.location.href = res.data.url;
} }
hasResponse = true; hasResponse = true;
} } else
break; throw 'Invalid parameters';
case 'css': break;
if (typeof res.data == 'string') case 'css':
{ if (typeof res.data == 'string')
//Check whether the requested file had already be included
if (!egw_json_files[res.data])
{ {
egw_json_files[res.data] = true; //Check whether the requested file had already be included
if (!egw_json_files[res.data])
{
egw_json_files[res.data] = true;
//Get the head node and append a new link node with the stylesheet url to it //Get the head node and append a new link node with the stylesheet url to it
var headID = document.getElementsByTagName('head')[0]; var headID = document.getElementsByTagName('head')[0];
var cssnode = document.createElement('link'); var cssnode = document.createElement('link');
cssnode.type = "text/css"; cssnode.type = "text/css";
cssnode.rel = "stylesheet"; cssnode.rel = "stylesheet";
cssnode.href = res.data; cssnode.href = res.data;
headID.appendChild(cssnode); headID.appendChild(cssnode);
} }
hasResponse = true; hasResponse = true;
} } else
break; throw 'Invalid parameters';
case 'js': break;
if (typeof res.data == 'string') case 'js':
{ if (typeof res.data == 'string')
//Check whether the requested file had already be included
if (!egw_json_files[res.data])
{ {
egw_json_files[res.data] = true; //Check whether the requested file had already be included
if (!egw_json_files[res.data])
{
egw_json_files[res.data] = true;
//Get the head node and append a new script node with the js file to it //Get the head node and append a new script node with the js file to it
var headID = document.getElementsByTagName('head')[0]; var headID = document.getElementsByTagName('head')[0];
var scriptnode = document.createElement('script'); var scriptnode = document.createElement('script');
scriptnode.type = "text/javascript"; scriptnode.type = "text/javascript";
scriptnode.src = res.data; scriptnode.src = res.data;
headID.appendChild(scriptnode); headID.appendChild(scriptnode);
} }
hasResponse = true; hasResponse = true;
} } else
break; throw 'Invalid parameters';
break;
case 'error':
if (typeof res.data == 'string')
{
this.jsonError(res.data);
hasResponse = true;
} else
throw 'Invalid parameters';
break;
}
} catch(e) {
this.jsonError('Internal JSON handler error', e);
} }
} }