Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return error message with actionResult

MVC App, client makes request to server, error happens, want to send the msg back to the client. Tried HttpStatusCodeResult but just returns a 404 with no message, I need the details of the error sent back to the client.

public ActionResult GetPLUAndDeptInfo(string authCode) {     try     {         //code everything works fine     }     catch (Exception ex)     {          Console.WriteLine(ex.Message);          return new HttpStatusCodeResult(404, "Error in cloud - GetPLUInfo" + ex.Message);     } } 
like image 527
John Avatar asked Feb 10 '14 16:02

John


People also ask

How do I return an error in ActionResult?

You should be logging the relevant information to your error log so that you can go through it and fix the issue. If you want to show the error in the form user submitted, You may use ModelState. AddModelError method along with the Html helper methods like Html.

How do I return an ActionResult model?

First of all, if you only return ViewResult from a particular Action, declare the method as returning ViewResult instead of ActionResult. var result = sut. Index(). ViewData.

How do you return a message in controller?

We can return the custom message from controller by throwing exception and handling it at client side using the ActionFailure event of the Grid. Grid Rendering Code. 2. Handle the returned message in the ActionFailure event of the Grid.

What is return type of ActionResult in MVC?

An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.


2 Answers

You need to return a view which has a friendly error message to the user

catch (Exception ex) {    // to do :log error    return View("Error"); } 

You should not be showing the internal details of your exception(like exception stacktrace etc) to the user. You should be logging the relevant information to your error log so that you can go through it and fix the issue.

If your request is an ajax request, You may return a JSON response with a proper status flag which client can evaluate and do further actions

[HttpPost] public ActionResult Create(CustomerVM model) {   try   {    //save customer     return Json(new { status="success",message="customer created"});   }   catch(Exception ex)   {     //to do: log error    return Json(new { status="error",message="error creating customer"});   } }  

If you want to show the error in the form user submitted, You may use ModelState.AddModelError method along with the Html helper methods like Html.ValidationSummary etc to show the error to the user in the form he submitted.

like image 118
Shyju Avatar answered Sep 17 '22 10:09

Shyju


One approach would be to just use the ModelState:

ModelState.AddModelError("", "Error in cloud - GetPLUInfo" + ex.Message); 

and then on the view do something like this:

@Html.ValidationSummary() 

where you want the errors to display. If there are no errors, it won't display, but if there are you'll get a section that lists all the errors.

like image 43
Mike Perrenoud Avatar answered Sep 17 '22 10:09

Mike Perrenoud