How to handle errors/exceptions returned by WCF servicer (REsTful) in Jquery ajax()method?
Assuming I have includeExceptionDetailInFaults="true" turned on and I use
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
only thing I get back is an HTML string (HTML page) with error buried deep inside it;;
I want to display errors like
- Service not found (HTTP 400 and 500
errrors) - .net exceptions thrown by WCF to the
client page (javascript)
How can I do that?
includeExceptionDetailsInFaultsis more of a debugging tool for unhanded exceptions than a way to actually deliver errors to the client. These kinds of errors would normally be 500 internal server errors whose details should not be leaked out to the client any way. For a REST service the way to generically deliver these details to the caller by default is to return them as text/HTML.However, if you are trying to return logical errors out of your service you should be conjuring up instances of
WebFaultException<T>where T can be a simple string message or, if you want something more complex, can be a class which will get serialized into a structured format according to whatever serialized is configured for your service endpoint. You are also able to set the response status code during the construction of theWebFaultException<T>.If you wanted generic handling of all unhandled exceptions to return a structured content type representation and status code you would need to write your own
IErrorHandlerand do the translation of the unhanded exception type to aWebFaultException<T>in yourProvideFaultimplementation.