I'm interested in implementing a proper exception handling for my WCF application. After a quick google ride, I've come across two solutions.
- Providing
FaultExceptions
for the client. - General exception handling using
IErrorHandler
.
What I didn't found, though, was a practical example that combines the two methods - if this is even considered as good practice.
My thoughts:
Throwing FaultExceptions with own detail types
throw new FaultException<StackOverflowFault>(
new StackOverflowFault(StackOverflowFaultCode.Rules,
"This question is not considered as constructive."));
Catching unhandled exceptions with IErrorHandler
and log them.
public bool HandleError(Exception ex) {
try {
SomeLogger.Error(ex.Message, ex.Source);
return true;
} catch (Exception) {
// Well, there was an exception handling the
// exception :(
// -> Drop the exception and throw a new one
throw new Exception("Unknown exception occured.");
}
}
...and provide faults based on the exception type
(not everything is for the clients eyes!)
public void ProvideFault(Exception error, MessageVersion version, ref Message fault){
if(error is FaultException) {
fault = Message.CreateMessage(version, ((FaultException<StackOverflowFault>)error).Reason)
} else if(error is SqlException) {
// What would Jon Skeet do?
}
}
My question
Is this considered as okay practice? And: If I'm already throwing FaultException in the application that are suitable for the client - does it makes sense to let them be handled by IErrorHandler
(which does it automatically)?