-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I'd like a better error message when the responde for /zkau isn't JSON.
I had several cases where network components returned HTML or similar to /zkau requests. In that case, I get this error:
Server can't process request. (']' Syntax Error).
which is bad. As a developer, I can activate tools in my browser to monitor network usage and look at the raw data. Also, the message is wrong; it's the client code in au.js which fails to parse the server response.
When this error happens rarely in production, I can only weep in frustration because I have no way to find out what happened - educating hundreds of office workers how to use web developer tools doesn't scale, especially when the error happens only rarely.
Is there a way to display the buggy server response?
Hi Aaron,
always nice hearing from you :)
As you mentioned the error happens when trying to parse the response into a JSON object. https://github.com/zkoss/zk/blob/8d4561145acd5f950a3adaac900c7b26ce461b37/zk/src/archive/web/js/zk/au.js#L179 and https://github.com/zkoss/zk/blob/8d4561145acd5f950a3adaac900c7b26ce461b37/zk/src/archive/web/js/zk/au.js#L61
The exception is handled in the catch block of onResponseReady: https://github.com/zkoss/zk/blob/8d4561145acd5f950a3adaac900c7b26ce461b37/zk/src/archive/web/js/zk/au.js#L248
This method cannot be overridden easily as it's not public and patching the .js file would be necessary.
The error message may be misleading in particular cases. It is most often caused by an incorrect redirect to a login or error page (which happens when the server has some kind of problem or is configured incorrectly and does not respond to the ajax request in a way the client side expects).
On the client side there is no way to find out what causes the redirect as it's handled transparently by the XMLHttpRequest.
To output the failed JSON string (e.g. an HTML document) the jq.evalJSON function can be overridden like this:
zk.afterLoad(function() {
var origEvalJSON = jq.evalJSON;
jq.evalJSON = function(jsonResponse) {
try {
return origEvalJSON.apply(this, arguments);
} catch (e) {
//your code here to display the problematic text
console.log('Error parsing JSON response:', e);
console.log(jsonResponse);
throw e;
}
};
});
Robert
Thanks, that feels like a feasible workaround. Since end users need to report the faulty JSON, I'm using this code:
zk.afterLoad(function() {
var origEvalJSON = jq.evalJSON;
var maxDialogs = 3; // prevent endless stream of error dialogs
jq.evalJSON = function(jsonResponse) {
try {
return origEvalJSON.apply(this, arguments);
} catch (e) {
if(maxDialogs > 0) {
maxDialogs --;
var msg = 'Error parsing JSON response; please report this error to [email protected]!\n' + jsonResponse;
zk.error(msg);
}
throw e;
}
};
});
Asked: 2016-07-22 12:28:57 +0800
Seen: 66 times
Last updated: Jun 14 '17