ColdFusion 10: Handling and Logging Errors With Ne

2019-04-14 22:52发布

问题:

I'm using ColdFusion 10's new REST API: http://www.adobe.com/devnet/coldfusion/articles/restful-web-services.html

Whenever there's an exception, the API handles it nicely and automatically returns something like this:

HTTP/1.1 500 Internal Server Error
Content-Length: 52
Content-Type: application/json
Date: Fri, 22 Feb 2013 01:07:49 GMT

{"Message":"Element FOO is undefined in ARGUMENTS."}

The problem is that neither of the following gets called:

  • Site-wide Error Handler
  • Application.cfc's onError

It seems like the REST API handles the error and the exception doesn't bubble up. I like to send myself an email (with error details) whenever server-side errors occur. Any thoughts on how I can do with the new REST API?

回答1:

It's not ideal, but you could add a try / catch in each method that sends you an email on error then rethrows.



回答2:

Site wide error handler works with REST services. Whiles invoking a REST service, user can specify the "Accept" Header. If the user specifies the accept header as xml or json, the error is caught and a struct with error code and message is serialized to appropriate format. In this case the site wide error handler is not invoked. It is not good to send the whole HTML content in JSON or XML. But if the requested Content-Type is text/html, then the Site wide handler is invoked and the HTML is returned as the response.



回答3:

I am not sure what you are using to consume the web service, but assuming it's ColdFusion, you can use cfhttp and its throwonerror attribute:

<cftry>
    <cfhttp
        url="http://localhost:8500/rest/restapp/crudService/1"
        method="get"
        timeout="5"
        throwonerror="yes" />

    <cfcatch type="Any">
        <!--- email yourself the error details --->
    </cfcatch>
</cftry>

That way you can use cfcatch to trap the 500 error that was returned and then email your self the cfcatch dump (or however you like to email error details).



回答4:

This is fixed, but only available in CF11.

Quote:

This is fixed in the latest ColdFusion11 release.

-- Hari Krishna Kallae

https://bugbase.adobe.com/index.cfm?event=bug&id=3506757

UPDATE

Hotfix for CF10 will be available on the next update (targeted for mid-Aug, 2014)