Search code examples
coldfusioncfconerror

Throwing error from cfc - cflocation doesn't work from inside onerror()


Edit: an important piece might be that I'm calling a cfc method via ajax...

I'm calling a cfc method via ajax, which returns data for use in a jqgrid table. Inside the cfc's method, I'm trying to set up error handling, so that instead of just failing and giving the user no indication of any problems, I send users to an error page, which is defined as error.cfm in the root directory, and which right now just includes some boilerplate html. Inside Application.cfc, I've defined my onError() method like so:

<cffunction name="onError" returnType="void" output="false"  >
    <cfargument name="exception" required="true"  >
    <cfargument name="eventname" type="string" required="true"  >
    
    <cfmail to="#application.REGISTRATION_NOTIFICATION#" from="outgoing address" subject="Error">
    
    </cfmail>

    <cflocation url="../index.cfm?file=error.cfm" addToken="false"  /> 

When I throw an error from the cfc's method, whether inside a cfcatch or not, Chrome reports that I have indeed been sent to index.cfm with a STATUS=200, and the response shows, correctly, the html that is inside the error.cfm template. However, the browser does not actually go to the page.

The thing is, if I change the location to

<cflocation url="index.cfm?file=error.cfm" addToken="false"  /> 

and throw an error from the page which was calling the cfc, instead of from the cfc, cflocation does work as expected. Also, if I use cfinvoke to call the cfc from the original page, instead of using the ajax call, cflocation works. So this must have something to do with the fact that I'm making the call to the cfc through ajax, instead of invoking it from within CF. So what am I missing here?

p.s. this happens in Chrome, IE, and FF. I'm using CF 9.0.1 with hotfixes, no frameworks. Here's a subset of the output from Chrome's Network pane, in Headers, for the referring page:

Request URL:http://localhost/sitename/cfc/method.cfc

Request Method:GET

Status Code:302 Moved Temporarily

Request Headers view source

Accept:application/json, text/javascript, /; q=0.01

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Connection:keep-alive

Cookie:CFID=11801; CFTOKEN=90037336; LASTVISIT=1327852981759

Host:localhost

Referer:http://localhost/sitename/index.cfm?file=referringfile.cfm

User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7

X-Requested-With:XMLHttpRequest

Query String Parameters view URL encoded

method:getPartNumbers

filter:{"page":1,"sessionSQL":"1327834969791","filterRows":[{"FIELD_NAME":"PART_NUMBER","OPERAND":"EQUALS","FIELD_VALUE":"11 06 01"}]}

_search:false

nd:1327835014639

rows:100

page:1

sidx:PART_NUMBER

sord:desc

Response Headers view source

Content-Type:text/html; charset=UTF-8

Date:Sun, 29 Jan 2012 11:03:34 GMT

Location:../index.cfm?file=error.cfm

Persistent-Auth:true

Server:Microsoft-IIS/7.5

Set-Cookie:LASTVISIT=1327853014657;expires=Fri, 27-Jul-2012 11:03:34 GMT;path=/

Transfer-Encoding:chunked

X-Powered-By:ASP.NET

And for the error.cfm page:

Request URL:http://localhost/sitename/index.cfm?file=error.cfm

Request Method:GET

Status Code:200 OK

Request Headers view source

Accept:application/json, text/javascript, /; q=0.01

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Authorization:Negotiate blahblahblahblah

Connection:keep-alive

Cookie:CFID=11801; CFTOKEN=90037336; LASTVISIT=1327716554490

Host:localhost

Referer:http://localhost/sitename/index.cfm?file=filename/filename.cfm&Config=filename

User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7

X-Requested-With:XMLHttpRequest

Query String Parameters view URL encoded

file:error.cfm

Response Headers view source

Content-Type:text/html; charset=UTF-8 Date:Fri, 27 Jan 2012 21:09:14

GMT Persistent-Auth:true Server:Microsoft-IIS/7.5

Set-Cookie:LASTVISIT=1327716554533;expires=Wed, 25-Jul-2012 21:09:14 GMT;path=/

Transfer-Encoding:chunked

X-Powered-By:ASP.NET


Solution

  • I think I've got my answer here:

    Handling Remote API Errors With Application.cfc's OnError Event Method

    Guess I know just enough Ajax to be dangerous :(

    I think what threw me is that I could see that cflocation was indeed redirecting to the error pages and that I could see the correct response in the network pane; but what I didn't realize is that since I was calling the function remotely, the browser will never actually go to the new window, and instead, the response is sitting there waiting to be handled by the ajax call itself. I'll figure this stuff out eventually.