PDA

View Full Version : How do you deal with error 500 responses to Ajax requests?



KallDrexx
18 Jan 2011, 12:06 PM
I have an ext form that I am submitting to my server. Due to a bug in my server code, an exception is being thrown and my web server's Error 500 page is being returned. The problem with this is my ext form is expecting a JSON response, but the Error 500 page is HTML, thus causing Ext to fail being unable to parse the '<' character.

How can I handle this situation? I want to display a message to the user that an error occurred, but this Ext exception is causing my failure handler not being call, as well as the form's waitMsg to not go away.

Graveworm
19 Jan 2011, 6:03 AM
I handle it in my SUCCESS-method of the request:


loadFormSuccess:function(form, result)
{
console.log('-->', result.response.status);
}

KallDrexx
19 Jan 2011, 6:17 AM
I handle it in my SUCCESS-method of the request:


loadFormSuccess:function(form, result)
{
console.log('-->', result.response.status);
}


I don't understand. The 500 response and/or the JSon parsing exception (probably the latter) is causing the success or failure handlers from being called.

mschwartz
19 Jan 2011, 6:29 AM
Fix the 500 error and it's not a problem? :)

Seriously though, it's something that's been discussed in other threads, that ExtJS might be able to catch error pages and throw up a dialog or something.

In the meantime, here's some code I wrote that is a simple approach:

http://code.google.com/p/helma-community/source/browse/trunk/helma-community/apps/Community/client/Core/rpc.js

You basically try/catch around everything on the server side and send back JSON with an indication an exception occurred on the server and whatever you want to show on the client. In my case, I'm showing the source code file with the line the exception occurred highlighted.

Another approach is based upon this page:
http://dev.sencha.com/deploy/dev/docs/?class=Ext.Ajax

Basically, you add beforerequest, requestcomplete, and requestexception handlers to Ext.Ajax (it's a singleton). In before request, you can add annotations (fields, members, whatever you want to call them) to the options object. In requestcomplete you can look at the response.status / response.responseText and examine it to see if it looks like an ASP/JSP/PHP/whatever error page (such as a compile error) and handle the error as you see fit.

The annotations I mentioned might be a flag, "this is a file upload and thus not true Ajax" so you don't see some JSON inside a <textarea> tag and think it's an error page.

KallDrexx
19 Jan 2011, 7:38 AM
Haha I do intend to fix the 500 error :P.

Your response did give me an idea, and after doing some research I think I found out how to return a JSON formatted 500 response for Ajax requests, which does solve the issue. Thanks!!

asagala
19 Jan 2011, 2:38 PM
Here is what I did to handle my 403 errors. You can probably apply for your 500 errors. Use the requestexception of Ext.Ajax. You only need to set this up once in your whole application


Ext.Ajax.on('requestexception', function(con, res, opt){
if(res.status =='403')
window.location= '/login';
}, this);

In your case you could do


Ext.Ajax.on('requestexception', function(con, res, opt){
if(res.status =='500')
//show error msg
}, this);

KallDrexx
21 Jan 2011, 10:36 AM
As an FYI, that didn't work with a error 500 occurring during form submission

asagala
22 Jan 2011, 6:05 AM
Yes you are right. Only works with Ajax requests using the Ajax object

Condor
22 Jan 2011, 8:08 AM
Normal FormPanel submits also use Ext.Ajax, unless you are using standardSubmit:true or isUpload:true.

For isUpload:true you can't detect the HTTP error status, so the error code must be passed in the response page.

valititi
22 Jan 2011, 10:33 AM
so, you have an answer from the server ? yes ? if yes, your problem is only that the returned text is some complex text that cannot fecth ok in the json response ? if only this is the problem, why you don't return some clean answer, like
{"success":"false","response":"yourVeryOwnExplanationAbout500Error"}

1. you will have a reponse page (that Condor suggest for such cases when cannot detect some "automatic" errors)

2. you got the answer at the line 12 (give as example by mschwartz - http://code.google.com/p/helma-commu...nt/Core/rpc.js (http://code.google.com/p/helma-community/source/browse/trunk/helma-community/apps/Community/client/Core/rpc.js)).