Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling of server-side HTTP 4nn/5nn errors returning a whole HTML document in jQuery's ajax requests
    text
    copied!<p>To the point: how would you handle a server-side HTTP 4nn/5nn errors in jQuery's ajax requests? This case concerns a JSP/Servlet webapplication at the server side. Here I am not talking about trivial runtime exceptions such as <code>NullPointerException</code> and so on. Assume that they're all handled perfectly. A good example of such a HTTP 4nn/5nn error is 401 unauthorized (insufficient user rights) and 500 internal server error (database down, I/O error, <a href="http://java.sun.com/javase/6/docs/api/java/lang/Error.html" rel="noreferrer"><code>Error</code></a>s, etc). Assume that they cannot (or should not) be caught at coding level.</p> <p>Right now I have just declared an <code>&lt;error-page&gt;</code> in <code>web.xml</code> for those kind of errors. It basically forwards the request to a predefinied JSP/HTML error page wherein the enduser is informed that a serious error has occurred and that the user can contact xx@xx.xx for further assistance. The same page also displays the global details about the error/exception.</p> <p>It works perfectly in regular HTTP requests, but how would you handle it in XMLHtttp requests using jQuery? What's the best for the user experience? To me, it would be just displaying the entire error page as if it is a normal HTTP request. I've solved it as follows:</p> <pre><code>function init() { $.ajaxSetup({ error: handleXhrError }); } function handleXhrError(xhr) { document.open(); document.write(xhr.responseText); document.close(); } </code></pre> <p>Although it works perfectly, it feels to me like a hack. Replacing the entire document with the contents of the HTTP error page. But is that also the way you would follow? If not, can you maybe elaborate why not and what way you'd prefer? The only alternative I see is using JS to display some alert/message box to inform the user about the unresolveable error, but the user <em>could</em> dismiss it and continue with the page while that should not be possible.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload