0

I am working on an application that uses Angular to get data from the user than passes it through an API built with ASP.net (I think it's MVC, but could be API) and into a SQL database.

What I'm wondering is how can I let the user know if they have entered some conflicting data with from the database? For instance, when they submit a form, a value called "Title" is a unique identifier in the SQL database. If someone enters a title called "Win" and someone later enters a title of "Win", i want them to receive an error saying that the title is already in use.

Currently I have:

$http.post(itemURL, Title).success(function (result){console.log("Good"})
.error(function (data){console.log(data);}

The error that gets logged to the console is the same I see in visual studio:

Object {Message: "An error has occurred.", ExceptionMessage: "An error occurred while updating the entries. See the inner exception for details.",

And when I check the inner exception I see that Title is in conflict. Is there a way I can use this error message to determine Title is the culprit? (Since there are a few other unique fields).

Even better would be a way of letting them know before they even hit the submit button that a value is in conflict, but I am pretty sure that is just wishful thinking.

EDIT: The error code that is coming back is 500 (internal server error), which is a little too general to be useful.

byteGeist
  • 73
  • 1
  • 6
  • 2
    The API needs to validate the data, and return the appropriate HTTP error code to the consuming AngularJS Client. A 409 in the event of a conflict. See: http://stackoverflow.com/questions/3290182/rest-http-status-codes-for-failed-validation-or-invalid-duplicate – Max Sorin Mar 14 '16 at 19:50
  • @MaxSorin- I am getting error 500, and that other question didn't really explain how I can set errors. Besides, my question really is how can I even check what error message I have in angular so I can print a message to the user? – byteGeist Mar 15 '16 at 19:47
  • You are getting the 500 because the API throws an exception, handle the exception. Return the correct code to the client. Consume the code client side and **don not** try to parse the exception message from the API. – Max Sorin Mar 16 '16 at 12:54

0 Answers0