Logging Clientside JavaScript Errors on Server

Logging Clientside JavaScript Errors on Server

You could try setting up your own handler for the onerror event and use XMLHttpRequest to tell the server what went wrong, however since it's not part of any specification, support is somewhat flaky.

Here's an example from Using XMLHttpRequest to log JavaScript errors:

window.onerror = function(msg, url, line)
{
var req = new XMLHttpRequest();
var params = "msg=" + encodeURIComponent(msg) + '&url=' + encodeURIComponent(url) + "&line=" + line;
req.open("POST", "/scripts/logerror.php");
req.send(params);
};

Log client side errors to the server

Since they're client-side errors, you'll have to send them to the server using XMLHttpRequest. You could use try...catch statements or window.onerror:

window.onerror = function (msg, url, line)
{
var message = "Error in "+url+" on line "+line+": "+msg;
$.post("logerror.aspx", { "msg" : message });
}

Be aware that line and url can be very inaccurate in IE versions prior to 8.

Error-logging for javascript on client side

Look into window.onerror. If you want to capture any errors, and report them to the server, then you could try an AJAX request, perhaps.

window.onerror = function(errorMessage, errorUrl, errorLine) {
jQuery.ajax({
type: 'POST',
url: 'jserror.jsf',
data: {
msg: errorMessage,
url: errorUrl,
line: errorLine
},
success: function() {
if (console && console.log) {
console.log('JS error report successful.');
}
},
error: function() {
if (console && console.error) {
console.error('JS error report submission failed!');
}
}
});

// Prevent firing of default error handler.
return true;
}

How to catch javascript exceptions/errors? (To log them on server)

I use this function in all my projects:

window.onerror = function(m,u,l){
jQuery.post("ajax/js_error_log.php",
{ msg: m,
url: u,
line: l,
window: window.location.href });
return true};

Make sure it is the very first javascript the browser receives or at least precedes any potentially error-causing code. Requires jQuery of course, but you could code ajax functions in pure javascript if you wanted to.

Please note: this will not help you if you have a syntax error. All javascript instantly dies if there is a syntax error.

Angularjs | How to log the client-side errors and send them to Server-side?

I assume you are mainly concerned about $http errors (non 2xx).

One way of doing that would be to configure global interceptor and perform some action in the 'responseError' handler:-

// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {

'responseError': function(rejection) {
// DO YOUR WAY OF HANDLING here.

return $q.reject(rejection);
}
};
});

$httpProvider.interceptors.push('myHttpInterceptor');

Your reponseErrorHandler can take care of non 2xx http statuses (5xx,4xx).
You might want to treat them differently as per your need.

You can trigger another ajax request here to your RESTful logging framework.

How to log Client Side exceptions in StackExchange.Exceptional?

As far as client side logging is concerned there is no mechanism as per my knowledge.
Because the developer never maintain any database of stuff on client side. Even if you save exception in client side storage that wont be a centralized approach and how will you be getting back again?

What you can do
By client side you can log javascript's specific scenarios/Exceptions via Ajax Calls to your server in the same error logging module that you are using for server error logs and saving those exception you want to log in your database to have their centralized record.



Related Topics



Leave a reply



Submit