|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Creating a custom ajax error
There are a couple of ways you can do this. I'm going to do a blogRaymond Camden 09/08/10 12:53 P That's exactly what I was looking for Ray. So the $.ajaxSetup() methodTony Bentley 09/08/10 01:04 P Hmm. Good followup!Raymond Camden 09/08/10 01:16 P FYI, blog entry on the work so far:Raymond Camden 09/08/10 01:59 P You're correct if the application was something on the public end but duringTony Bentley 09/08/10 02:00 P So I plan a blog entry on this, but taking my example of ajaxSetup:Raymond Camden 09/08/10 02:20 P This is what I got from today's lesson ...Tony Bentley 09/08/10 02:38 P My post above is wrong. Here is what I wanted to happen:Tony Bentley 09/08/10 02:58 P Remove the dump perhaps.Raymond Camden 09/08/10 03:27 P Has anyone used cfhttp to throw an ajax error that returns a JSON object and error code 500? I'm looking for a code snippet of your onError() method. I'm using jQuery and am also trying to decide how to handle it on the client side. Specifically looking for when a session expires and when a server error occurs what to do on the client side (session expiration refreshes the page to login panel,etc). There are a couple of ways you can do this. I'm going to do a blog entry on this in a few minutes with a full example, but, you can have logic like this: (pseudo-code obviously for this part) if I need to login: if ajax request, throw an exception with a specific message else redirect to login.cfm Then in your JS code, make use of the global Ajax object to see errors: $.ajaxSetup({ error:function(x,e){ if(x.status == 500 && x.statusText == "SessionTimeout") { alert("Your session has timed out."); location.href = 'index.cfm'; } } }); > > Has anyone used cfhttp to throw an ajax error that returns a JSON object and error code 500? I'm looking for a code snippet of your onError() method. I'm using jQuery and am also trying to decide how to handle it on the client side. Specifically looking for when a session expires and when a server error occurs what to do on the client side (session expiration refreshes the page to login panel,etc). That's exactly what I was looking for Ray. So the $.ajaxSetup() method handles the errors and redirects expired session. I knew there was a way to do this outside of my ajax requests but wasn't sure if I needed to create a proxy method or otherwise. Also, I noticed that you are expecting a string "SessionTimeout" which is exactly what I am doing, just not in the same place (hence asking for some advice since I am duplicating this all over the place). So this leaves me with another thought about passing the error back to the client. If a ColdFusion error occurs, I would really like to have a structure to work with but how would you identify that it indeed is a server error? I would want an indicator and a structure at the same time....or maybe you can advise this as well? Thanks for that snippet. Hmm. Good followup! So I'd say you would a) want to handle the issue for sure, but b) probably not tell the user too much. So assuming that you normally have an error handler that say something vague (or maybe it shows the full error, whatever, point is, you handle it), we need a version for Ajax too. Correct? (I think I just restated what you said - but want to ensure we are on the same page. ----- Excess quoted text cut - see Original Post for more ----- FYI, blog entry on the work so far: http://www.coldfusionjedi.com/index.cfm/2010/9/8/Example-of-handling-session-time-outs-in-an-Ajax-application ----- Excess quoted text cut - see Original Post for more ----- You're correct if the application was something on the public end but during black box testing is when this would be useful. Before go-live I would like the user (or another client based delivery system) to catch errors and pass them back to a ticket. I can then go into my error log files and see more detail of what happened or get a little more information about how to duplicate and resolve the issue. Following, I would return something vague once black box testing is complete like 'were sorry an error has occurred' and then refresh or catch it and move on, etc. So to join the two, the real answer is yes and yes :) I would want to say whatever I want and handle it however I want based on when in the SDLC it is occurring and who is getting the error. Thanks for helping me on this. I know there are a lot of ways to do it but that's my problem. I think there is probably a better solution specific to CF/JQuery, perhaps the most common client/server combination for ColdFusion. So I plan a blog entry on this, but taking my example of ajaxSetup: $.ajaxSetup({ error:function(x,e){ if(x.status == 500 && x.statusText == "SessionTimeout") { alert("Your session has timed out."); location.href = 'login.cfm'; } } }); I'd simply add an else to the IF there to do X,where X is either the vague handler or the more verbose one you spoke of. ----- Excess quoted text cut - see Original Post for more ----- This is what I got from today's lesson ... Application.cfc - OnError: <cffunction name="onError" access="public"> <cfargument name="exception" required="true"> <cfargument name="EventName" type="String" required="true"> <!---if the request is ajax, throw an error that can be returned to the client in a basic string and then log the error as usual---> <cfif StructKeyExists(requestHeaders, "X-Requested-With") and StructFind(requestHeaders,"X-Requested-With") eq "XMLHttpRequest"> <cfthrow errorcode="500" message="ColdFusion Error"> <cfelse> <cfdump var="#arguments.exception#"> </cfif> </cffunction> Client side script: $.ajaxSetup({ error:function(x,e){ if(x.status == 500 && x.statusText == "SessionTimeout") { alert("Your session has timed out."); location.href = 'login.cfm'; } else if(x.status == 500 && x.statusText == "ColdFusion Error"){ alert(x.statusText);//test for coldfusion error } } }); Thanks Ray. This is what I needed today. My post above is wrong. Here is what I wanted to happen: <cffunction name="onError" access="public"> <cfargument name="exception" required="true"> <cfargument name="EventName" type="String" required="true"> <!---if the request is ajax, throw an error that can be returned to the client in a basic string and then log the error as usual---> <cfif StructKeyExists(requestHeaders, "X-Requested-With") and StructFind(requestHeaders,"X-Requested-With") eq "XMLHttpRequest"> <cfdump var="#arguments.exception#"> <cfheader statusCode="500" statusText="ColdFusion Error"> <cfelse> <cfdump var="#arguments.exception#"> </cfif> </cffunction> This throws the error and dumps it out in html for firebug but ajaxSetup() now knows what to do with it based on x.status == 500 && x.statusText=="ColdFusion Error". Remove the dump perhaps. ----- Excess quoted text cut - see Original Post for more -----
|
February 08, 2012
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||