I am trying to show the exception that is being thrown because of SQL store procedure failure.I am getting the response in ajax but that is to with requst.responseText but I dont want to access the whole responseText.I need only the title of this responseText
My Controller is as Follows:
public ActionResult ReOrderSegment(string DocumentIDs, long DossierIDForReOrder)
{
try
{
var TrimmedString = DocumentIDs.TrimEnd('#');
var DocumentIDsWithInteger = Convert.ToInt64(DocumentIDs.Split('#').FirstOrDefault());
long SelectedCompany = db.Documents.FirstOrDefault(x => x.ID == DocumentIDsWithInteger).CompanyID;
var Message = db.PerformAutomatedSorting(SelectedCompany, DossierIDForReOrder, TrimmedString);
return Json(Message, JsonRequestBehavior.AllowGet);
}
catch (SqlException ex)
{
Response.StatusCode = 500;
Response.TrySkipIisCustomErrors = true;
return Json(new { errorMessage = ex.Message }, JsonRequestBehavior.AllowGet);
}
return Json(0);
}
and JS:
$.ajax({
type: 'POST',
url: rootUrl("Dossier/ReOrderSegment"),
dataType: "json",
data: { DocumentIDs: DocumentIDs, DossierIDForReOrder: DossierIDForReOrder },
traditional: true,
success: function (rest)
{
alert(rest);
},
error: function (request, status, error)
{
//var all = JSON.parse(request.responseText);
alert(request.responseText);
}
});
});
here i am attaching the images
here i want to show only "Please select only Air Product that are UnInvoiced" in alert.
Here is how I am doing it which you may find helpful.
In base controller:
protected virtual JsonResult JsonError(Exception error, string message = null, int statusCode = 500)
{
JsonNetResult res = new JsonNetResult();
res.ContentType = "application/json";
res.ContentEncoding = System.Text.Encoding.UTF8;
res.Data = new JsonErrorModel(){ error = error, message = message };
if (error != null)
Log.Error(message, error);
else if (!string.IsNullOrWhiteSpace(message))
Log.Error(message);
int newStatus = statusCode;
if (error != null)
{
if (error is ArgumentException || error is ArgumentNullException || error is AbsenceSoftException)
newStatus = 400;
var aggErr = error as AbsenceSoftAggregateException;
if (aggErr != null)
{
newStatus = 400;
message = message ?? string.Join("; ", aggErr.Messages);
}
}
if (Response != null)
{
Response.StatusCode = newStatus;
Response.StatusDescription = message ?? (error == null ? "An error has ocurred" : error.Message);
}
return res;
}
in HomeController:basecontroller
public JsonResult TestCase(Model model)
{
try
{
}
catch (Exception ex)
{
return JsonError(ex);
}
}
However, I am thinking to re-factor it, and to implement it as a reusable exception handling attribute or possibly managing the error handling code logic in global.asax.cs application_error method.
You can use following code in your error block:
var newTitle = $(request.responseText).filter('title').text(); // or try find('title').text();
The variable newTitle above should show you the title.
But Ideally you should try creating a small class for ErrorDetails with properties like a string Message, or a List Messages to save all your validation messages and return to the client like this in your catch block in controller:
ErrorDetails errorDetails = new ErrorDetails();
errorDetails.Message = "Please select only Air Product that are UnInvoiced";
return Json(errorDetails, JsonRequestBehavior.AllowGet);
And In jquery code, read the error details like this:
error: function(xhr, status, error) {
var obj = $.parseJSON(xhr.responseText); //can also use xhr.responseJSON directly instead of parsing it.
alert(obj.Message);
//use this obj to to read message or list of messages by for loop.
}