locked
How to print useful error messages when WinJS.xhr could not connect?

    Question

  • Currently when you call WinJS.xhr with an invalid URL the error handler will be called with the XHR object. However we have no way to print out the reason for the error as the XHR object itself does not carry this information with it.

    All xhr does is to print an error to the console:

    SCRIPT7002: XMLHttpRequest: Network Error 0x2ee7, Could not complete the operation due to error 00002ee7.
    File: default.html

    How to catch this error in the app?

    Tuesday, November 27, 2012 10:57 AM

All replies

  • Hi!

    have you tried this:

    WinJS.xhr(options).done(
            function completed(request) {
                // handle completed download.
            }, 
            function error(request) {
                // handle error conditions.
            }, 
            function progress(request) {
                // report on progress of download.
            }
        });
    
    Regards!

    Tuesday, November 27, 2012 6:22 PM
  • Of course. Try to call an invalid URL and see for yourself what the request contains in the error handler. No useful information about why the request failed.
    Tuesday, November 27, 2012 8:31 PM
  • In short, if status of the failed request is 0, you can assume that some kind of network level error has happened. I don't think there is a way to tell more than that (i.e. unreachable destination, dns failure, etc..).


    According to W3C Standards:

    The status attribute must return the result of running these steps:

    1. If the state is UNSENT or OPENED, return 0 and terminate these steps.

    2. If the error flag is set, return 0 and terminate these steps.

    3. Return the HTTP status code.

    ...

    In case of DNS errors, TLS negotiation failure, or other type of network errors, this is a network error. Do not request any kind of end user interaction.

    This does not include HTTP responses that indicate some type of error, such as HTTP status code 410.


    In other words, if there is an application level-error, like 404, you will have its code in the status field and user-friendly message in the statusText field. If there is initialization or network error, you will have 0 in the status.


    As a side note1: WinJS.xhr executes same error handler for both application and network-level errors. However raw XMLHttpRequest slightly differentiates them such that it fires onerror event in case of network-level error and readyStateChanged in case of successful completion or application-level error.


    As a side note2: Executing synchronous XMLHttpRequest actually gives more human friendly message, but still a generic one.

    var req = new XMLHttpRequest();
    
    try {
        req.open("GET", "http://zzz.xxx.ccc", false);
        req.send();
    }
    catch (err) {
        console.log(err.message);
    }

    The err here is DOMException with message saying "NetworkError".





    • Edited by AndreyR1231 Tuesday, December 04, 2012 2:51 AM
    • Proposed as answer by AndreyR1231 Tuesday, December 04, 2012 4:43 PM
    Tuesday, December 04, 2012 2:41 AM
  • Thanks. It's unfortunate that the async API is so much worse in reporting connection errors. You would think the W3C would have thought of that.
    Tuesday, December 04, 2012 12:38 PM
  • Hi Phil_ke,

     I've got your identical error but incredibly calling a function that return WinJS.xhr(...) give me an error, generic, but an error!

    Speackin' in code mode:

    WinJS.xhr(options).done(
           
    function completed(request) {
               
    // handle completed download.
           
    },
           
    function error(request) {
               
    // handle error conditions.
           
    },
           
    function progress(request) {
               
    // report on progress of download.
           
    }
       
    });

    doesn't rise any error.

    changing to this:

    callWsExternal(options) {
                return WinJS.xhr(options);       
    }
    
    callWsExternal(options).done(
       function completed(request)
       {
            // handle completed download.
       }, 
       function error(request)
       {
            // handle error conditions.
       }, 
       function progress(request)
       {
           // report on progress of download.
       }
     });

    rise network errors (500 or 404)...don't tell me why...


    • Edited by ZaffaSte Thursday, November 28, 2013 11:13 AM
    Thursday, November 28, 2013 11:10 AM