I have a WCF service which I am using with a WPF application.
I am trying to achieve error handling so that the service will provide instructions to the user depending on the error i.e. (e.g. code error writes to log and informs user to contact support) vs (e.g. trying to write duplicate value into a unique field informs user of field with duplicate and asks to change value and try again ... no log entry)
I have successfully created my error DataContract and I can get the data in my WPF application. In fact the whole system works as desired. My problem however is the following section of code is playing havoc with my OCD. It seems hack and slash.
try
{
db.SaveChanges();
}
catch (Exception ex)
{
String message = "";
if (ex.InnerException.ToString()
.Contains("Cannot insert duplicate key row") == false)
{
logError le = new logError();
le.log(ex, "AddTemplate");
message = "An error has occured,
please contact you support representative";
}
else
{
message = "Code already exists";
}
throw new FaultException<myError>
(new myError { Operation = "Add Template",
ProblemType = message });
}
Is there a better way of identifying the error in order to send a specific message to the client other than what I have done here. The String.Contains part is the part that really bothers me.