Reputation: 4513
I've got a WCF REST project and I want to catch the errors that the WCF framework throws and display them in my way (JSON, that is).
For example, If I'm expecting an int parameter in my call and I get a string, the framework would display a page with "request error" and some trace info... I'd just like to get the exception and display it in my JSON format as response to the user.
Just to make this more clear - I'm not looking to catch an exception IN CODE, but an exception that happens outside of the code. an exception that the WCF would generate it self, such as (when I passed a string into an int field)
The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service. The exception message is 'Input string was not in a correct format.'. See server logs for more details. The exception stack trace is:...
Any ideas how this can be done?
Many thanks in advance!
Upvotes: 3
Views: 1853
Reputation: 28530
It sounds like you're looking to implement global error handling in the WCF service, so you can catch unhandled exceptions (either an unanticipated error in your code or something outside your control). If my understanding is correct, you'll want to look into the IErrorHandler Interface.
There's a number of blog posts and articles on how to impement IErrorHandler. Here's one that covers both SOAP and REST: WCF Exception Handling with IErrorHandler.
Upvotes: 0
Reputation: 6302
You need to use FaultContracts to generate your own custom errors. Takes a bit of time to setup but once you have it, it works wonders. The good thing about it is that the FaultContract is completely serializable.
[OperationContract]
[FaultContract(typeof(CustomFault))]
string WillThrowArgumentException();
Here is a really good explanation of getting it to work.
http://blog.ngommans.ca/index.php?/archives/33-Handling-custom-errors-in-WCF.html
Upvotes: 1