Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery ajax() handling WCF errors?

Tags:

jquery

wcf

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

  1. Service not found (HTTP 400 and 500 errrors)
  2. .net exceptions thrown by WCF to the client page (javascript)

How can I do that?

like image 299
Cshah Avatar asked Oct 12 '22 01:10

Cshah


1 Answers

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.

like image 132
Drew Marsh Avatar answered Oct 13 '22 14:10

Drew Marsh