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.
*

View File

@ -204,14 +204,24 @@ egw_json_request.prototype.alertFunc = function(_message, _details)
alert(_message);
}
function _egw_json_debug_log(_msg)
function _egw_json_debug_log(_msg, _e)
{
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 */
egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRequest)
{
@ -219,6 +229,8 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
{
var hasResponse = false;
for (var i = 0; i < data.response.length; i++)
{
try
{
var res = data.response[i];
@ -233,7 +245,8 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
res.data.message,
res.data.details)
hasResponse = true;
}
} else
throw 'Invalid parameters';
break;
case 'assign':
//Check whether all needed parameters have been passed and call the alertHandler function
@ -247,7 +260,8 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
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
@ -271,7 +285,8 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
_egw_json_debug_log(e);
}
hasResponse = true;
}
} else
throw 'Invalid parameters';
break;
case 'jquery':
if (typeof res.data.select == 'string' &&
@ -287,7 +302,8 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
_egw_json_debug_log(e);
}
hasResponse = true;
}
} else
throw 'Invalid parameters';
break;
case 'redirect':
if (typeof res.data.url == 'string' &&
@ -307,7 +323,8 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
}
hasResponse = true;
}
} else
throw 'Invalid parameters';
break;
case 'css':
if (typeof res.data == 'string')
@ -326,7 +343,8 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
headID.appendChild(cssnode);
}
hasResponse = true;
}
} else
throw 'Invalid parameters';
break;
case 'js':
if (typeof res.data == 'string')
@ -344,8 +362,20 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
headID.appendChild(scriptnode);
}
hasResponse = true;
}
} else
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);
}
}