Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to report error to $.ajax without throwing exception in MVC controller?

I have a controller, and a method as defined...

[HttpPost] public ActionResult UpdateUser(UserInformation model){     // Instead of throwing exception    throw new InvalidOperationException("Something went wrong");      // I need something like     return ExecutionError("Error Message");     // which should be received as an error to my     // $.ajax at client side...  } 

Problems with Exceptions

  1. We have to log exceptions in case of device or network errors like SQL Connectivity errors.
  2. These messages are like validation messages for users, we dont want to log.
  3. Throwing exceptions also floods Event Viewer.

I need some easy way to report some custom http status to my $.ajax call so that it should result an error at client side, but I do not want to throw an error.

UPDATE

I cannot change client script because it becomes inconsistent with other data source.

So far, HttpStatusCodeResult should work but it's IIS that is causing the problem here. No matter what error message I set, tried all answers, still I receive default message only.

like image 563
Akash Kava Avatar asked Jan 02 '12 14:01

Akash Kava


People also ask

How do you handle errors in Ajax call?

The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.

What triggers Ajax error?

Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the . ajaxError() method are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

How can make Ajax call in MVC?

Open your Visual Studio and create a empty ASP.NET MVC application. Click on File -> New Project -> Web -> ASP.NET web application. From the next window Select template Empty and from Add folders and core reference choose MVC. Name it as AJAXCalls and click Ok.

What is an Ajax error 0?

Status code 0 means the requested url is not reachable.


2 Answers

This is where HTTP status codes come into play. With Ajax you will be able to handle them accordingly.

[HttpPost] public ActionResult UpdateUser(UserInformation model){     if (!UserIsAuthorized())         return new HttpStatusCodeResult(401, "Custom Error Message 1"); // Unauthorized     if (!model.IsValid)         return new HttpStatusCodeResult(400, "Custom Error Message 2"); // Bad Request     // etc. } 

Here's a list of the defined status codes.

like image 115
Dennis Traub Avatar answered Oct 07 '22 14:10

Dennis Traub


Description

What about returning an object back to your page and analyse that in your ajax callback.

Sample

[HttpPost] public ActionResult UpdateUser(UserInformation model) {     if (SomethingWentWrong)         return this.Json(new { success = false, message = "Uuups, something went wrong!" });      return this.Json(new { success=true, message=string.Empty}); } 

jQuery

$.ajax({   url: "...",   success: function(data){     if (!data.success)      {        // do something to show the user something went wrong using data.message     } else {        // YES!      }   } }); 
like image 42
dknaack Avatar answered Oct 07 '22 16:10

dknaack