Greetings. I'm thoroughly confused about the gazillion ways of handling
errors in CXF/JAX-RS. I've read whatever documentation I've found on the
wiki, and have followed the many posts to the mailing list on this
subject, and I still can't figure out how to cover all corner cases
without hacking the CXF source code (a situation I don't want to get
into). 

I need to capture 100% of all possible errors that can ever happen
inside CXF, the web container (Tomcat 6.X), and any possible CXF
application running in the container (we ONLY use REST based services
using CXF), and stream the errors/exceptions out as XML (we use XStream
for that). That's the requirement. 

Here's what I've done:

1) I wrote a piece of code that implements the ResponseHandler
interface. I do quite a bit of analysis inside that code, and return a
custom response called an ErrorResponse. This eventually gets streamed
out to XML. This ResponseHandler is configured as a provider with the
<jaxrs:providers> mechanism in our Spring config file. It seems to cover
the cases where the error condition is wrapped inside a Response object.

2) I implemented a bunch of ExceptionMapper classes for the most common
exceptions, as well as a "catch-all" for other exceptions. This seems to
cover the cases where a service is throwing an exception. My mappers
convert the exception to XML and stuffs that in a Response object. The
mappers are also configured using the <jaxrs:providers> mechanism. 

3) I wrote an a piece of code that extends the
AbstractOutDatabindingInterceptor class (based on the CXF example that
Sergey posted a while back). This is configured using the following
mechanism:
    
<cxf:bus>
  <cxf:outFaultInterceptors>
    <ref bean="XMLOutFault.Interceptor"/>
  </cxf:outFaultInterceptors>
</cxf:bus> 

4) I wrote a Servlet Filter (implementing the javax.servlet.Filter
interface), that catches any exception and streams it out as XML. The
problem is that nothing downstream seems to throw any exceptions, so the
doFilter() method always returns normally. 

I need to know what I have to do to cover 100% of all possible error
cases, and stream the error out using our custom XStream transcode back
to the client. Some code samples or fairly detailed info about how to
write the code, and how to wire it up would be fantastic. One of the
corner cases NOT covered by any of the above mentioned mechanisms is
when I specify a URL that is not mapped by CXF. In this case, the
org.apache.cxf.transport.servlet.ServletController's invoke() method
will call generateNotFound(), which sets the HTTP status code to 404,
and writes an error message directly to the HttpServletResponse. How can
I override this? 

I don't want ANY component other than my code, writing ANYTHING to the
servlet's OutputStream in case something goes wrong. I absolutely have
to control 100% of all error handling. Any tips, tricks, and pointers to
examples are greatly appreciated. Thanks much,

/Henrik


Reply via email to