Hi

I think the JAX-RS spec is going to talk more about handling custom
exceptions in the 0.8 or later versions but as far as I'm aware the most
portable way at the moment is to throw a (runtime)
WebApplicationException.
Provided your resource class is a thin wrapper around a more involved
application class, you might want to catch all the application exception
and wrap them into a WebApplicationException like this :

try {
 doIt();
} catch (ABCException ex) {
    throw new
WebApplicationException(Response.status(appropriateErrorStatus).entity(t
oXML(ex.getMesssage())).build()); 
}    

This way you'll get the XML message as expected and it will work across
multiple JAX-RS implementations

JAX-RS dictates that other exceptions like ABCException (provided
they're runtime ones ?) should be propagated to the container such that
one can write say a servlet filter and handle them, but at the moment
they're caught at the binding level...There's likely to be more
clarification in this regard in the future versions of the spec

Cheers, Sergey
 

-----Original Message-----
From: yarddog [mailto:[EMAIL PROTECTED] 
Sent: 21 April 2008 20:48
To: cxf-user@incubator.apache.org
Subject: JAX-RS and Exception / Fault Handling


I'm looking for an example or documentation referencing the appropriate
way
to handle exceptions in a custom manner on a JAX-RS server.

I have a REST service resembling the following:

@Path("/exception")
@GET
public String getException() throws ABCException{
    throw new ABCException("Test exception message");
}

When I invoke this service, I receive the following response:

<ns1:XMLFault xmlns:ns1="http://cxf.apache.org/bindings/xformat";>
    <ns1:faultstring
xmlns:ns1="http://cxf.apache.org/bindings/xformat";>test.rest.ABCExceptio
n:
Test exception message</ns1:faultstring>
</ns1:XMLFault>

I'd like to be able to catch this exception (probably via an interceptor
so
that I can handle all exception for a jaxrs server in a general fashion)
and
return a response resembling the following:

<error>
    <message>Test exception message</message>
</error>

What I have done to date is written an ExceptionInterceptor and
registered
it as an outFaultHandler.  I've tried removing the exception from the
MessageContentsList and adding my custom Error object as an elementData
object, but it appears I'm to late in the process (phase) to affect the
output.  When I do this, the default CXF error message no longer
displays,
but my Error object is not marshalled.  I'm not even convinced that the
outFaultHandler is correct, as the phase INVOKE (as part of inHandler)
seems
more appropriate.  I'll admit, I've just been trying various
combinations to
make it work.

Can anyone point me in the right direction?

Thanks - 
Steve Ardis


-- 
View this message in context:
http://www.nabble.com/JAX-RS-and-Exception---Fault-Handling-tp16810827p1
6810827.html
Sent from the cxf-user mailing list archive at Nabble.com.

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Reply via email to