0

Better error message when zkau response isn't JSON

asked 2016-07-22 12:28:57 +0800

digulla gravatar image digulla
506 5

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?

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-07-25 07:59:59 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2017-06-14 08:56:41 +0800

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

link publish delete flag offensive edit
0

answered 2016-07-25 09:05:36 +0800

digulla gravatar image digulla
506 5

updated 2016-07-27 14:30:20 +0800

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;
        }
    };
});
link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2016-07-22 12:28:57 +0800

Seen: 66 times

Last updated: Jun 14 '17

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More