简体   繁体   中英

How to exclude stacktrace from axis2 fault response

I have an Axis2 web service which throws different detail messages in the fault response to signal problems in the call.

At some point, due to server errors (others than the ones treated by the web service), in the fault detail string I get the full stacktrace of what happened. I do not want the client to see the stack trace, so (as a catch all errors) I want to output a simple "Server error" message with no stacktrace, no nothing.

What is the simplest way of intercepting fault responses and changing the fault message. Are modules the only way of (complicated) doing this?

Or, is there a configuration in Axis2 that says not to display stacktrace in fault?

Thanks!

I once had a similar problem. Not sure if there is some config to turn off the stacktrace showing, at least none that I could find at that moment (that would have been the best solution). Instead, I opted for a quick and dirty approach, mostly due to lack of time.

What I did was to provide Axis2 with the detail of the fault myself. The Axis2 servlet has a method called handleFault which deals with generating the fault. More exactly (deeper in the call) the MessageContextBuilder.createFaultEnvelope method is used to construct the fault element.

Having the stacktrace in the detail is the default behavior, but there are ways to specify your custom detail. One way is to use the the AxisFault 's detail field in which you can add an OMElement (refer to AXIOM ) to be placed into the fault. So you do something like:

public class MyServlet extends AxisServlet {
  ...
  public void handleFault(MessageContext msgContext, OutputStream out, AxisFault e) {
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMElement detail = factory.createElement(...);
    e.setDetail(detail);
    // now let axis do its thing with the new improved AxisFault
    super.handleFault(msgContext, out, e);
  }
}

Now, instead of the exception stacktrace, your detail will be added instead.

Axis2 uses Apache commons logging and the AxisFault messages that you are seeing are generated by code in Axis2 that looks similar to:

 try { executeMethod(httpClient, msgContext, url, getMethod); handleResponse(msgContext, getMethod); } catch (IOException e) { log.info("Unable to sendViaGet to url[" + url + "]", e); throw AxisFault.makeFault(e); } finally { cleanup(msgContext, getMethod); } 

[This code segment comes from org.apache.axis2.transport.http.HTTPSender]

So refer to apache commons logging user guide for instructions on how to set the logging levels and destination of the messages.

Hope this helps.

Can you not just catch the AxisFault

try {
    // do stuff
} catch (AxisFault f) {
    log.error("Encountered error doing stuff", f);
    throw new IOException("Server error");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM