House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Creating a custom ajax error

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Tony Bentley
09/08/2010 11:53 AM

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).

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/08/2010 12:53 PM

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).

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Tony Bentley
09/08/2010 01:04 PM

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.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/08/2010 01:16 PM

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 -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Tony Bentley
09/08/2010 02:00 PM

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.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/08/2010 02:20 PM

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 -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Tony Bentley
09/08/2010 02:38 PM

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.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Tony Bentley
09/08/2010 02:58 PM

   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".

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group

<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

February 08, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29