Reputation: 86627
I have a little soap webservice. And I wonder how I can response with errors.
Eg:
public int createUser(String username) {
//create...
return id;
}
Now, if the user could not be created, how do I make a proper response? Do I have to throw a custom exception? Which is the right way?
Upvotes: 3
Views: 2660
Reputation: 9428
I do this in my SOAP server - but don't fool around with trying to build your own Soap fault - simply throw a runtime exception and that should get converted into a soap fault.
Upvotes: 1
Reputation: 2364
My experience with this is that we created a common class that gets returned along with the data class. So for example, the soap response comes back with a CommonOut object and a ResultData object. The CommonOut class contains fields that are populated with data whether the request is good or bad such as Status, Records Returned, and Message.
An interceptor is used to catch all exceptions in the container and wrap them in our custom format and returned to the user. That way you can catch the real exception messages and wrap them in your own format.
The danger of returning the real error messages to the caller is that it could expose details about your system potentially unless you catch exceptions and override the values that get returned.
This is similar to the SOAP fault mentioned above, but gives you more ability to customize your output format.
Upvotes: 0
Reputation: 159754
The correct approach here is to translate the exception into a SOAP Fault.
Upvotes: 1