Reputation: 6130
I am creating my first ASP.NET MVC2 Project with using jquery and ajax on client side.
Now when i was having Q.A testing the application and add some test exception and pass it to json from my controller that something like this:
Controller:
public JsonResult PopulateDetails(int id)
{
ProductServiceClient client = new ProductServiceClient();
try
{
if (id == 0)
{ throw new Exception("TEST ERROR."); }
string name= "test";
List<Product> product= client.GetPriductbyName(name);
return Json(product);
}
catch (Exception ex)
{
throw ex;
}
}
Jquery-Ajax Script:
$.ajax({
type: "POST",
url: url_ ,
data: search,
success: function(data) { // data: Passed (Records) from the PopulateDetails controller
displayDetails(data); // after routing on Populate Details converts the data to table
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("error: " + XMLHttpRequest.responseText);
},
dataType: 'json'
});
I notice that it will return this default error message below:
---------------------------
Windows Internet Explorer
---------------------------
error: <html>
<head>
<title>TEST ERROR.</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Lucida Console";font-size: .9em}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>TEST ERROR.</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
<br><br>
<b> Exception Details: </b>System.Exception: TEST ERROR.<br><br>
<b>Source Error:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
Line 64: catch (Exception ex)
Line 65: {
<font color=red>Line 66: throw ex;
</font>Line 67: }
Line 68: }</pre></code>
</td>
</tr>
</table>
<br>
---------------------------
OK
---------------------------
Now how to display just the error message from the exception neatly not the whole html error message. Something like simple as display of an alert message box alert("TEST ERROR").
Is there ways how to do it more simpler?
I saw this linked on asp.net which look good How Do I Handle jQuery Errors and Display them on the Client?
but i cant find where AjaxResponseViewModel() came declared.
Upvotes: 3
Views: 2067
Reputation: 20674
In your catch, you should probably log the error and then return a json friendly result, e.g.
catch (Exception ex)
{
//log.Error(ex); using whatever you use to log
return Json(ex.Message);
}
or
return Json("There has been an error, please contact support or read the log");
Above you are throwing the exception which will return the error view in full.
Upvotes: 3