Log Javascript Error in Elmah - AngularJs
End users will not report any javascript error. That would be tough for us to handle in case any. In this scanerio we need to handle javascript also in Server side. Through the help of Elmah we can handle the same
Below API Service logs the any errors to the Elmah by posting.
public class ErrorLogController : ApiController
{
public HttpResponseMessage PostByLog([FromBody]string exception)
{
try
{
ErrorLog.GetDefault(null).Log(new Error(new Exception(exception)));
return this.Request.CreateResponse(HttpStatusCode.Accepted);
}
catch (Exception e)
{
return this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError,e.Message);
}
}
}
In Angular Module write the code for Handle the errors using $ExceptionHandler and post the errors to Api Service.
var commonModule=angular.module("Common",[]);
commonModule.factory("$exceptionHandler", function () {
return function (error, cause) {
/* TODO: Check if possible to use $rootScope, $http is have circular dependency */
var exception = error + Error().stack;
return $.ajax({
method: 'POST',
url: '/api/ErrorLog',
data: '=' + exception
});
};
});Written by Janarthanan
Related protips
1 Response
You can use $injector to retrieve http service.
var http = $injector.get('$http');
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Did you know?
AngularJS pages are long-lived browser apps with the same script risk as a newer single-page app. Third-party code in the template can read forms and values rendered on the page.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#