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
How can I do that?
includeExceptionDetailsInFaults
is 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 the WebFaultException<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 IErrorHandler
and do the translation of the unhanded exception type to a WebFaultException<T>
in your ProvideFault
implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With