This project is read-only.

Custom Exception For Malformed Request

Topics: Web Api
Jul 13, 2011 at 4:54 PM

When a resource method accepts parameters, the framework tries to provide those parameters from the request.  When it cannot, it throws an InvalidOperationException with a message like:

The service operation 'Post' expected a value assignable to type 'MyCustomMessage' for input parameter 'myMessage' but received a value of type 'HttpRequestMessage`1'.

Throwing an exception is good.  However I would like to treat this kind of exception differently than unhanded exceptions from my code.  In a public service it's good practice to hide the details of general exceptions from clients, for security reasons.  But in this case I want to tell the client something specific -- that they probably made a mistake. 

How about making this a custom exception type that I can test for in my global error handler?