javascript - Custom handle exception does not return my custom JSON -


i have asp.net mvc app, , implemented custom handleexceptionattribute when app throw ajax exception data.responsejson not have json.

here handleexceptionattribute

public class handleexceptionattribute : handleerrorattribute {     public override void onexception(exceptioncontext filtercontext)     {         if (filtercontext.httpcontext.request.isajaxrequest() && filtercontext.exception != null)         {             guid guid = guid.newguid();             string guidstr = guid.tostring();             filtercontext.httpcontext.response.statuscode = (int)httpstatuscode.internalservererror;             filtercontext.result = new jsonresult             {                 jsonrequestbehavior = jsonrequestbehavior.allowget,                 data = new                 {                     success = false,                      error = "error al procesar su solicitud, por favor comuniquese con un administrador con el siguiente codigo: " + guidstr,                     stacktrace = filtercontext.exception.stacktrace                 }             };             filtercontext.exceptionhandled = true;              var properties = new dictionary<string, string> { { "error guid", guidstr } };             var telemetry = new telemetryclient();             // send exception telemetry:             telemetry.trackexception(filtercontext.exception, properties);         }         else         {             base.onexception(filtercontext);         }     } } 

and here javascript function use exception show error

function addactividad() { ...     $.ajax({         url: 'iniciativasestrategicaswebpart/addactividad',         type: 'post',         data: {             idobjetivo: idobjetivovalue,             idiniciativa: idiniciativavalue,             iniciativaname: iniciativavalue,             fase: fase,             nombreactividad: nombreactividad,             fechainicio: fechainicio,             fechavencimiento: fechavencimiento,             estado: estado,             responsables: responsables         }     }).done(function (data) {         drawfasesinactividades(data);     }).fail(function (data) {         showerrormessage(data);     }); } function showerrormessage(data) {     $("#divalertiniciativas").attr("class", "alert alert-danger");     $("#divalertiniciativas").text(data.responsejson.error);     $("#alertiniciativascontainer").show("slow");     location.href = "#alertiniciativascontainer"     settimeout(         function () {             $("#alertiniciativascontainer").hide("slow");         }, 8000); } 

stacktrace:

   @ microsoft.sharepoint.client.clientrequest.processresponsestream(stream responsestream)    @ microsoft.sharepoint.client.clientrequest.processresponse()    @ microsoft.sharepoint.client.clientrequest.executequerytoserver(chunkstringbuilder sb)    @ microsoft.sharepoint.client.clientrequest.executequery()    @ microsoft.sharepoint.client.clientruntimecontext.executequery()    @ microsoft.sharepoint.client.clientcontext.executequery()    @ business.businessbusiness.controllers.iniciativasestrategicaswebpartcontroller.addactividad(actividad actividad) in d:\pc\business\developers\dev1\business\business\controllers\businesswebpartcontroller.cs:line 222    @ lambda_method(closure , controllerbase , object[] )    @ system.web.mvc.actionmethoddispatcher.execute(controllerbase controller, object[] parameters)    @ system.web.mvc.reflectedactiondescriptor.execute(controllercontext controllercontext, idictionary`2 parameters)    @ system.web.mvc.controlleractioninvoker.invokeactionmethod(controllercontext controllercontext, actiondescriptor actiondescriptor, idictionary`2 parameters)    @ system.web.mvc.async.asynccontrolleractioninvoker.actioninvocation.invokesynchronousactionmethod()    @ system.web.mvc.async.asynccontrolleractioninvoker.<begininvokesynchronousactionmethod>b__39(iasyncresult asyncresult, actioninvocation innerinvokestate)    @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresult`2.callenddelegate(iasyncresult asyncresult)    @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end()    @ system.web.mvc.async.asyncresultwrapper.end[tresult](iasyncresult asyncresult, object tag)    @ system.web.mvc.async.asynccontrolleractioninvoker.endinvokeactionmethod(iasyncresult asyncresult)    @ system.web.mvc.async.asynccontrolleractioninvoker.asyncinvocationwithfilters.<invokeactionmethodfilterasynchronouslyrecursive>b__3d()    @ system.web.mvc.async.asynccontrolleractioninvoker.asyncinvocationwithfilters.<>c__displayclass46.<invokeactionmethodfilterasynchronouslyrecursive>b__3f()    @ system.web.mvc.async.asynccontrolleractioninvoker.asyncinvocationwithfilters.<>c__displayclass46.<invokeactionmethodfilterasynchronouslyrecursive>b__3f()    @ system.web.mvc.async.asynccontrolleractioninvoker.asyncinvocationwithfilters.<>c__displayclass46.<invokeactionmethodfilterasynchronouslyrecursive>b__3f()    @ system.web.mvc.async.asynccontrolleractioninvoker.<>c__displayclass33.<begininvokeactionmethodwithfilters>b__32(iasyncresult asyncresult)    @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresult`1.callenddelegate(iasyncresult asyncresult)    @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end()    @ system.web.mvc.async.asyncresultwrapper.end[tresult](iasyncresult asyncresult, object tag)    @ system.web.mvc.async.asynccontrolleractioninvoker.endinvokeactionmethodwithfilters(iasyncresult asyncresult)    @ system.web.mvc.async.asynccontrolleractioninvoker.<>c__displayclass21.<>c__displayclass2b.<begininvokeaction>b__1c()    @ system.web.mvc.async.asynccontrolleractioninvoker.<>c__displayclass21.<begininvokeaction>b__1e(iasyncresult asyncresult) 

the solution found change handleexceptionattribute , set statusdescription because filtercontext.result not work in scenarios.

public class handleexceptionattribute : handleerrorattribute {     private bool isajax(exceptioncontext filtercontext)     {         return filtercontext.httpcontext.request.headers["x-requested-with"] == "xmlhttprequest";     }     public override void onexception(exceptioncontext filtercontext)     {         if (filtercontext.exceptionhandled || !filtercontext.httpcontext.iscustomerrorenabled)         {             return;         }          // if request ajax return json else view.         if (isajax(filtercontext))         {             guid guid = guid.newguid();             string guidstr = guid.tostring();             filtercontext.httpcontext.response.clear();             filtercontext.httpcontext.response.statuscode = (int)httpstatuscode.internalservererror;             filtercontext.httpcontext.response.statusdescription = "error al procesar su solicitud, por favor comuniquese con un administrador con el siguiente codigo: " + guidstr;             filtercontext.exceptionhandled = true;              var properties = new dictionary<string, string> { { "error guid", guidstr } };             var telemetry = new telemetryclient();             // send exception telemetry:             telemetry.trackexception(filtercontext.exception, properties);         }         else         {             //normal exception             //so let handle default ways.             base.onexception(filtercontext);          }     } } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -