-1

Following on from this question jQuery Ajax error handling, show custom exception messages I am trying to display to my users a custom error message from ColdFusion (via AJAX) when a form is submitted but an error has occured.

I have tried to do this using a standard <cfthrow errorcode="500" message="You did something wrong"/> which would return back to jQuery an response status code that is not 200 hence its easy to show the error like such:

...
error: function (xhr, textStatus, thrownError) {
                        $('p').text(thrownError);
                    }

However, in ColdFusion production servers it is recommended to turn off error reporting in the debug settings. So now all my user sees is "Internal server error" instead of the custom message.

My temporary solution now is to just do a <cfreturn "Error: You did something wrong"/> <cfabort> and then make jQuery look for any returned result from the server where the text starts with "Error:".

I would much rather use proper exception handling with <cfthrow>. How can this be achieved given the recommendation to turn debugging off?

Community
  • 1
  • 1
volume one
  • 6,800
  • 13
  • 67
  • 146
  • 1
    Is the response a JSON packet? If so, include a `success` property and/or `error` message in the JSON and check for those values when response is received on client side. – Scott Stroz Sep 14 '16 at 14:48
  • So this would always be handled in the `success()` method of the ajax call - which is what I'm doing now but looking for the string of `Error:` in the returned result from ColdFusion. It just seems wrong to handle errors in the success method. – volume one Sep 14 '16 at 16:08
  • It is wrong, for several reasons including SEO. If the page is a 200 success, there's nothing telling the search crawler that the page response is in error. – Kevin B Sep 14 '16 at 16:12
  • @KevinB Which part is wrong? – volume one Sep 14 '16 at 16:24
  • ... I kinda pointed that out in my comment. – Kevin B Sep 14 '16 at 16:27
  • 2
    I always look at `success` as meaning there was no server error. Form validation is not a server error, nor does it mean that there wasn't a successful request/response. – Scott Stroz Sep 14 '16 at 17:27
  • 1
    From the jQuery docs for `success`: A function to be called if the request succeeds. If you submit a form with missing or incorrect data and you handle this with proper form validation, that does not mean there was an unsuccessful request. – Scott Stroz Sep 14 '16 at 17:31
  • @ScottStroz A typical example of an error would be the user submitting an invalid URL for an image they want to use as part of their post. That URL can't be accessed by ColdFusion's ``. ColdFusion therefore has to return an error to the user telling them that their URL is malformed. Would you still say this is best handled in the `success()` function given the problem that CF's error reporting is off? – volume one Sep 14 '16 at 20:13
  • 1
    To me, that is still part of form validation and not a server error. – Scott Stroz Sep 15 '16 at 12:00

2 Answers2

1

I had similar problem. Try this please: First make sure "Enable HTTP status codes" unchecked in CF Admin under Server Settings -> Settings. You might need to restart your ColdFusion sever. That will prevent CF to set ALL statuses to either 404 or 500. Then add a line below to your server code (modify it according to your exception/code).

<cfthrow type="Authentication" errorcode="450" message="this is message">

That should give you 450 status code in your javascript error handler.

Leonid Alzhin
  • 164
  • 2
  • 8
-1

<cfthrow> errorCode is not the HTTP status code response. If you do throw an uncaught error, the default is to respond with a 500 HTTP status code regardless of the errorcode in your <cfthrow>. In this case, you'd want to use the .error() method of the jquery.ajax() to display a message to the user. More often than not, you'd want to avoid throwing an error intentionally and just include a 500 status code header. Alternatively as you indicated in your question, you can return with some value that is conditionally handled in the .success() of your $.ajax().

J.T.
  • 2,606
  • 15
  • 31
  • Yes but the problem is that the `error()` method can't display the error message because its error reporting is turned off in the ColdFusion administrator on production servers — hence my dilema/question. – volume one Sep 14 '16 at 16:06
  • If you want to return the error from the server to the client then you need to catch the error and return error or just error.message. There is nothing natural about having CF giving exception info to javascript. There is a reason exception info is usually turned off on a production system. – J.T. Sep 14 '16 at 16:53