Misha Zaslavsky
Misha Zaslavsky

Reputation: 9626

WCF Which messages to return after catching an exception?

I am using a wcf service and I know how to catch all the exceptions I need... But I dont know which messages should I return?

My code:

try
{
    currentPosition = await locator.GetGeopositionAsync();
}
catch (FaultException<MessageError> ex)
{
    MessageBox.Show(...?);
}
catch (EndpointNotFoundException ex)
{
    MessageBox.Show(...?);
}
catch (CommunicationException ex)
{
    ...
}
catch (Exception ex)
{
    ...
}

I can return ex.Message but I dont want the client to know all the details, I want to show a short and helpful message.

What should I do?

Upvotes: 0

Views: 74

Answers (1)

Sheridan
Sheridan

Reputation: 69959

I have always handled this situation in a similar way to the way that @Tim suggested in his comment. I need as much information to be saved so that I can debug the problem at a later date, but as you said, we don't want to show the end user the developer Exception messages.

Therefore the solution that I use is simply to store the information that comes from the Exception in the database and to provide the user with 'user-friendly' error messages. Make sure that you also add code to record any inner Exceptions as well if they exist. Now to address the question as to what to put in these messages... it really depends on your situation, but I generally follow this procedure.

First, I work out (either from forward thinking or from test results) which are the most likely errors to occur... off the top of my head, I'm talking about errors like 'no network access', or 'invalid user security', etc.

When I have a number of possible errors, I will attempt to catch the exact errors with a number of catch statements as you have. Inside the handlers, I check for certain Exception messages and return pre-written user friendly messages for each. Finally, I add one last generic message to display for all unforeseen error situations.

Upvotes: 1

Related Questions