I’ve created a REST API in MVC3 and one the things I need to do is return a descriptive message when an update or create operation fails.
Messages like "The update operation failed while creating the customer object." isn’t good enough.
My next thought was to get the message out of the exception which returns something like this:
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Business_Category". The conflict occurred in database "MyDb_Dev", table "dbo.Category", column 'CategoryID'.
The statement has been terminated.
Which seems to be too much information. I could parse out column "CategoryID"out of the exception message… Not sure if that is reliable.
Is it possible to set up attributes for the fields so when they fail I can get that fields error description? I’m open to suggestions.
Sounds like you need to use Exception.Data. More info can be found here:
http://msdn.microsoft.com/en-us/library/system.exception.data.aspx