CXF - exception handling and response with my custom message - web-services

I have developed web service using Apache CXF and now I want to deal with exception handling mechanism. Is there any way to configure it like some general listener (aspect, interceptor, whatever...) waiting for any runtime exception. and when it occurs will send my custom ResponseType message to user. This custom message is, it's clear, type from XSD scheme.

It is good to define fault for each operation you expose. Avoid throwing run time exception instead handle it in service itself.

Related

How to disable Verbose Error Messages of soap web services?

How to disable Verbose Error Messages of soap web services?
I have one web method with some para.
Example Req:
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<GenerateTestRequest xmlns="http://tempuri.org/">
<sId>1#!aB</sId>
<mobileNum>12345678</mobileNum>
<srcType>1</srcType>
</GenerateTestRequest>
</Body>
</Envelope>
When an unexpected input was supplied into sId para instead of Int32, Then the web service returned an exception within the response content.
Example Res:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</faultcode>
<faultstring xml:lang="en-SG">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:sId. The InnerException message was 'There was an error deserializing the object of type System.Int32. The value '1#!aB' cannot be parsed as the type 'Int32'.'. Please see InnerException for more details.</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
I don't want to display response content of exception message. Is there any way to turn off stack trace or exception details?
Is there any way to turn off stack trace or exception details?
Yes, by doing proper error handling for the SOAP web service.
A SOAP request can receive back a successful SOAP response or can receive back an unsuccessful response in the form of a SOAP Fault. People unfortunately focus on the successful cases only and let the framework they are using handle other problems.
Because SOAP is a protocol any exception on the server needs to be converted to a proper Fault response. This needs to be done by the developer by catching any exception and building appropriate Fault responses out of them, with only the needed data inside. For security reasons of course that data can't contain stacktraces or others sensitive information. But like I said, people often neglect to handle the error cases, and the framework can't build custom Faults, it can only build a SOAP fault with details from the exception that occurred, exposing it to the client.
You didn't tag this WCF but the schema namespace on the error code says windowscommunicationfoundation so I'll assume that's the web service framework. You can replace all exception messages with a generic error message by setting IncludeExceptionDetailInFaults to false (I think for your service it is set to true right now). See here for details: either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server
Or, you can do proper error handling and define and document the possible Faults a client can receive and convert any exception to one of those Faults. You can start reading here: Specifying and Handling Faults in Contracts and Services
If you are using any other web service framework or library, the idea is the same, the documentation to read will be different.

Trace all RPC calls in C++ gRPC server

I have a gRPC server written in C++ and I would like to trace or log all RPC calls to the server, including arguments and responses, if possible.
The Go gRPC implementation has the very helpful concept of an Interceptor that can be attached to a client or a server. The interceptor gets access to not only the metadata, but also to the arguments/responses. For the C++ API I cannot find anything similar.
What about https://grpc.github.io/grpc/cpp/classgrpc_1_1experimental_1_1_interceptor.html ?
The only usage reference I could find is this SO question: C++ grpc::experimental:interceptor how to return status and message from custom interceptor
EDIT: in matter of fact, I think this is a better resource - https://github.com/grpc/grpc/blob/7bf82de9eda0aa8fecfe5edb33834f1b272be30b/test/cpp/end2end/server_interceptors_end2end_test.cc
I think you can query the request & response messages from the methods argument of the interceptor overridden method Intercept(grpc::experimental::InterceptorBatchMethods* methods), see available methods and properties here: https://grpc.github.io/grpc/cpp/classgrpc_1_1experimental_1_1_interceptor_batch_methods.html

Cxf interceptors for http exceptions

We are using jaws:client to consume a web service. We have inFaultInterceptor that captures soap faults for further processing. But this inFaultInterceptor is not being invoked for http errors like 404 or ConnectException. We configured interceptors to jaws:client. Can we write any interceptor that captures these http exceptions? Is there any better way to capture them? All we want is to take the control back when such exception/error occurrs.
Note: we cannot catch them in web service code due to some restrictions.
You can use a FaultListener added to CXF Bus. The listener will capture the http exceptions allowing you to execute your code before they are raised to invoking method.
ClientProxy.getClient(jaxWSClientProxy).getBus().getProperties().put("org.apache.cxf.logging.FaultListener",new CxfFaultListenerImpl());
public class CxfFaultListenerImpl implements FaultListener{
public boolean faultOccurred(final Exception exception,final String description,final Message message) {
//return false to avoid standard CXF logging of exception
return false;
}
}

SOAP Webservice call from Java gives "Object reference not set to an instance of an object"

I have a requirement where I call a SOAP based web service from Java using Axis2 from eclipse. The web service code is in C#, with a BasicHttpBinding.
But when I call the method from the client stub I get this error.
org.apache.axis2.AxisFault: Object reference not set to an instance of an object.
Could anyone help me figure out this one? Is this on the service side or on the client side? Previously I got 'Internal Server error' and then they had to add something so that I can see this error in the logs.
The message is from the C# web service side ("Object reference not set to an instance of an object" is basically a Java equivalent of NullPointerException) but it might be because of something you send from your Java client or maybe you don't send.
The error usually means that you didn't send a required parameter and that the web service didn't do a proper job of validating it's input and missing parameter got to a point when caused the NullReferenceException.
But there is only one way to be sure, and that is to troubleshoot the call.
I suggest you use something like SoapUI to create a message and send that to the service. Once you get a succesfull call in SoapUI, make a call with the same parameters from your Java client and see what happens. When you do that, using a proxy for logging is very useful to see if the sent message is actually the expected one.

Handle WCF exceptions from webservice proxy

I have a WCF service (.NET 4) hosted in IIS. My service contract has one method with:
[FaultContract(typeof(string))]
My service implementation for that method validates input parameters and may throw an exception like this:
if (string.IsNullOrWhiteSpace(siteName)) { throw new FaultException("siteName is required."); }
The consumer program of this service is a .NET 2 console application. In this app I generated the webservice proxy by right clicking "References" and "Add web reference".
When I try to call the service method from the client app, by sending an invalid siteName, I get back a 503 Service Unavailable exception, instead of a FaultException with the custom error message.
How can I make the FaultException, with the custom error message, propagate to the client program?
UPDATE:
The exception I get in the client application is this:
System.ServiceModel.ServerTooBusyException:
The HTTP service located at
http://www.example.com/services/myservice.svc
is too busy. ---> System.Net.Web
Exception: The remote server returned
an error: (503) Server Unavailable.
at
System.Net.HttpWebRequest.GetResponse()
at
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpCha
nnelRequest.WaitForReply(TimeSpan
timeout) --- End of inner exception
stack trace ---
The problem was being caused by a custom HTTP module in our web project. So it was not exactly a WCF problem, but a problem specific to our solution. Thanks!