I’m confused on how I should return errors to a client using SOAP.
I have a wcf service, but I’m not sure what technology the client is using, so I’d like to stick to the SOAP specification.
As far as I’ve read, Fault messages seem to be the best way to handle this.
I can see my service having many different possible faults:
- Null\expected data errors
- Data format errors (ie: db only allows 3 chars)
- Data Range errors “Customer already exists”, “Unable to process your request” type errors
Would it be proper to create a new object for each of those faults and throw as such:
FaultException<NullFault>(nf);
FaultException<InvalidDataFault>(idf);
FaultException<ArguementFault>(af);
FaultException<RangeFault>(rf);
The client is passing in big objects with many properties to practically every method
(ie: Customer, Order, etc.).
Would this be the proper way of handling errors and sending back to the client?
It seems out of place to have to add each fault to the attributes above the method.
IE:
[OperationContract]
[FaultContract(typeof(NullFault))]
[FaultContract(typeof(InvalidDataFault))]
[FaultContract(typeof(ArguementFault))]
[FaultContract(typeof(RangeFault))]
void CreateCustomer(Customer customer);
Also, what about a GenericFault? How would you handle business rule errors? ie: Customer already exists, too many line items, don’t ship to that area, payment method not accepted, etc.?
Please let me know if this method is proper or if there is another accepted solution and how you would handle the ‘business rule’ situation.
Returning a Fault is the correct way to do it in general, but you should also ask yourself what the client program is going to do with the information you return in a fault. If the client only needs to know that a fault happened then all you need is a single fault. That fault could include message text that the client could display to its user.
If the client needs to take different action based on whether it’s a NullFault or a RangeFault, then that’s when you need separate faults. But if there’s no difference between one and another, then save yourself and the client programs some time and effort and only define a single fault.