WebServices Exception Handling

Hello All,

is there a best pratice how to handle exceptions in webservices as provider and consumer.
e.g.: handle the exception in the flow and return to caller only an error code but no exception or throw an exception?

When you are working with Webservices you need to take care about two aspects, SOAP exception (fault) and the service exception.

Handling of soap exception becomes neccessary while acting as consumer, as the return can be SOAP-Response or SOAP-Fault so proper handling shd be done. This can be done by braching on soap-fault string…
Apart from SOAP fault, service exception/error handling is required to catch and throw custom/application/system errors.

error handling sometimes depends on the customer for webservices and there are several scenarios, say we have the below scenario
Consumer → ESB Producer → ESB Consumer → Actual Producer

It is good to have error block in soap response message to pass the errors softly.

At ESB Consumer:

  • validation errors, data errors in ESB or from the application can be handled and Error code should be assigned (better not to throw exception).
  • soap faults, connection issues should be handled in ESB and ultimate consumer shall be provided with Error code either softly or by throwing exception
  • In case of ESB runtime exceptions, catch them and throw exception
    a. could be re-try exception if ESB consumer is not part of synchronous chain
    b. if ESB Consumer is part of sync chain, then add error codes for re-try/no retry and throw exception

At ESB producer:

  • validation errors, data errors in ESB or from the application can be passed to consumer in softly w/o throwing exception (with error codes)
  • In case of ESB runtime exceptions, catch them, add Error code to error (or just error code) based on error
  • For exceptions thrown by ESB Consumer or ESB runtime issues in ESB producer, catch them in ESB producer then we have two options in ESB Producer
    a. an exception can be thrown to generate a soap fault for ultimate consumer
    b. supress the errors and pass in error bloack within the response message

Apart from this it is always good to maintain list of errors that can be re-tried as in some realtime applications we do not want to re-try within ESB but inform consumer that this request can be re-tried again.

in general, always better to handle exception/errors and process as per our requirements.