I have a MVC project that makes a call to a WCF service. The WCF service method, in the case of business rule failure, throws its own exception, CarTooOldException. This has a string that I want to show to the user, "Car is too old".
public class CarTooOldException : Exception
{
public CarTooOldException(string message) : base(message)
{ }
}
My MVC code will catch an exception but the type is a FaultException. The message inside is correct however ("Car is too old").
Without changing the WCF service, is there a way to detect that this is a CarTooOldException? A call to .GetType() doesn't tell me.
How can I be sure that if I see this FaultException that the message is a user-friendly one and safe to show the user?
Right now I fear my only choice is to look a the strings the WCF method throws and do a .Contains() on the message property.
EDIT: The method literally does
if (age > MAX_AGE)
throw new CarTooOldException("Car is too old")
Thanks to @3dd for pointing me at that SO question. I understand that I cannot determine what the type is, because the developer that wrote the WCF service did not do FaultException.
I shall be looking to do a string comparison instead, as I don't want to change the service (legacy code).