You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID (ATTSI)" <dk...@att.com> on 2011/03/23 17:08:49 UTC

Avoiding stack trace for particular WebApplicationExceptions & 500 instead of 403

I have a condition I need to check for in my CXF REST service that may result in having to throw back an error with status 403 (forbidden).  I'd like to log this occurred, but I don't need to have a stack trace appear in my log.


I had tried to make this happen, but it's still putting the WebApplicationException stack trace into my log.

Another possibly related problem is that the client isn't getting a 403, it's getting a 500.

In my controller handler, when I detect the condition, I do this:

  throw new WebApplicationException(Response.Status.FORBIDDEN)

I've defined the following:
---------------
public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException> {
    private static Logger  logger  = Logger.getLogger(WebApplicationExceptionMapper.class);

    @Override
    public Response toResponse(WebApplicationException ex) {
        if (ex.getResponse().getStatus() == Response.Status.FORBIDDEN.getStatusCode())
            logger.error("Request failed with FORBIDDEN status code.");
        else
            logger.error(ex);
        return null;
    }
}
---------------

In my debugger, I saw it throw the exception and the first if block, but in my console it shows the following (slightly elided) exception:

<Mar 23, 2011 8:57:45 AM PDT> <Error> <HTTP> <BEA-101020> <[ServletContext@8348067[app:shop module:/apiservice path:/apiservice spec-version:2.5]] Servlet failed with Exception
java.lang.RuntimeException: org.apache.cxf.interceptor.Fault
        at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:102)
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:315)
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)
        at org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:97)
        at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461)
        Truncated. see log file for complete stacktrace

Caused By: org.apache.cxf.interceptor.Fault
        at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:155)
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:121)
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:153)
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:87)
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
        Truncated. see log file for complete stacktrace

Caused By: javax.ws.rs.WebApplicationException
        at mypackage.MyClass.checkCondition(MyClass.java:NN)

RE: Avoiding stack trace for particular WebApplicationExceptions & 500 instead of 403

Posted by "KARR, DAVID (ATTSI)" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Wednesday, March 23, 2011 10:06 AM
> To: users@cxf.apache.org
> Subject: Re: Avoiding stack trace for particular
> WebApplicationExceptions & 500 instead of 403
> 
> Hi
> 
> On Wed, Mar 23, 2011 at 4:08 PM, KARR, DAVID (ATTSI) <dk...@att.com>
> wrote:
> 
> > I have a condition I need to check for in my CXF REST service that
> may
> > result in having to throw back an error with status 403 (forbidden).
> I'd
> > like to log this occurred, but I don't need to have a stack trace
> appear in
> > my log.
> >
> >
> > I had tried to make this happen, but it's still putting the
> > WebApplicationException stack trace into my log.
> >
> > Another possibly related problem is that the client isn't getting a
> 403,
> > it's getting a 500.
> >
> > In my controller handler, when I detect the condition, I do this:
> >
> >  throw new WebApplicationException(Response.Status.FORBIDDEN)
> >
> > I've defined the following:
> > ---------------
> > public class WebApplicationExceptionMapper implements
> > ExceptionMapper<WebApplicationException> {
> >    private static Logger  logger  =
> > Logger.getLogger(WebApplicationExceptionMapper.class);
> >
> >    @Override
> >    public Response toResponse(WebApplicationException ex) {
> >        if (ex.getResponse().getStatus() ==
> > Response.Status.FORBIDDEN.getStatusCode())
> >            logger.error("Request failed with FORBIDDEN status
> code.");
> >        else
> >            logger.error(ex);
> >        return null;
> >    }
> > }
> > ---------------
> >
> >
> In the custom exception mapper you need to return Response, probably:
> 
> return ex.getResponse();
> 
> With CXF filters returning a non-null Response means the request was
> blocked
> or overridden (in case of out filters), but with exception mappers
> Response
> should be returned.
> 
> Cheers, Sergey

That did it, thanks.

Re: Avoiding stack trace for particular WebApplicationExceptions & 500 instead of 403

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

On Wed, Mar 23, 2011 at 4:08 PM, KARR, DAVID (ATTSI) <dk...@att.com> wrote:

> I have a condition I need to check for in my CXF REST service that may
> result in having to throw back an error with status 403 (forbidden).  I'd
> like to log this occurred, but I don't need to have a stack trace appear in
> my log.
>
>
> I had tried to make this happen, but it's still putting the
> WebApplicationException stack trace into my log.
>
> Another possibly related problem is that the client isn't getting a 403,
> it's getting a 500.
>
> In my controller handler, when I detect the condition, I do this:
>
>  throw new WebApplicationException(Response.Status.FORBIDDEN)
>
> I've defined the following:
> ---------------
> public class WebApplicationExceptionMapper implements
> ExceptionMapper<WebApplicationException> {
>    private static Logger  logger  =
> Logger.getLogger(WebApplicationExceptionMapper.class);
>
>    @Override
>    public Response toResponse(WebApplicationException ex) {
>        if (ex.getResponse().getStatus() ==
> Response.Status.FORBIDDEN.getStatusCode())
>            logger.error("Request failed with FORBIDDEN status code.");
>        else
>            logger.error(ex);
>        return null;
>    }
> }
> ---------------
>
>
In the custom exception mapper you need to return Response, probably:

return ex.getResponse();

With CXF filters returning a non-null Response means the request was blocked
or overridden (in case of out filters), but with exception mappers Response
should be returned.

Cheers, Sergey


> In my debugger, I saw it throw the exception and the first if block, but in
> my console it shows the following (slightly elided) exception:
>
> <Mar 23, 2011 8:57:45 AM PDT> <Error> <HTTP> <BEA-101020>
> <[ServletContext@8348067[app:shop module:/apiservice path:/apiservice
> spec-version:2.5]] Servlet failed with Exception
> java.lang.RuntimeException: org.apache.cxf.interceptor.Fault
>        at
> org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:102)
>        at
> org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:315)
>        at
> org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)
>        at
> org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:97)
>        at
> org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461)
>        Truncated. see log file for complete stacktrace
>
> Caused By: org.apache.cxf.interceptor.Fault
>        at
> org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:155)
>        at
> org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:121)
>        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:153)
>        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:87)
>        at
> org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
>        Truncated. see log file for complete stacktrace
>
> Caused By: javax.ws.rs.WebApplicationException
>        at mypackage.MyClass.checkCondition(MyClass.java:NN)
>