5

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Any practicle example of IErrorHandler with Jquery ajax() method?
IErrorHandler has no strong ties to any specific client library. You make a decision on what "shape" you want the error details object to be that you return to the client and then you can read that in the jQuery failure handler. For example, the simplest thing to do would be to make IErrorHandler::Provide fault just return WebFaultException<string> where you simply put the exception.ToString() into it with 400 bad request. Then in jQuery you would just show what what returned by reading XHR's responseText property.
WebFaultException is in 4.0 only isn't it?
@AlexanderN - yes, that's correct. Pre-4.0 you have to roll this solution yourself with a custom Exception subclass that a custom IErrorHandler can watch for -OR- set your own failed HTTP status/body in try/catches within your own methods which is... yuck. :)
0

I haven't tried this specifically with a request to the WCF, but this works for calls to WebMethod within an ASP.NET aspx page:

            error: function(e) {
                var errorObject = JSON.parse(e.responseText);
                if (errorObject.ExceptionType === 'MyNamespace.MyException') {
                   //do stuff for MyException
                }
                else {
                    //do regular exception handling
                }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.