dennisgorelik: (2009)
Dennis Gorelik ([personal profile] dennisgorelik) wrote2016-02-07 03:45 am

Javascript errors report

When users open my web site I want to know what JavaScript errors users have (if any).
That's why I append this javascript to almost every page on my web site:
window.onerror = function(errmessage, errurl, errline) {
	var params = {
		list: [],
		add: function(name, value) {
			if (value != null) this.list.push(name + '=' + encodeURIComponent(value));
			return this;
		},
		toString: function() {
			if (this.list.length) return '?' + this.list.join('&');
			return '';
		}
	};
	new Image().src='/jeh' + params.add('errmessage', errmessage)
		.add('errurl', errurl)
		.add('errline', errline)
		.add('r', Math.floor((Math.random() * 10) + 1));
}
That script reports javascript errors from user browser back to our server.
Once per day our server aggregates these errors and emails to developers Javascript Errors report.
This is an example of what that report looks like:


We then review these errors on case-by-case basis and decide whether we want to fix that error or we want to suppress that error from report (because we can not fix it).

Still, there are challenges: sometimes it is hard to separate errors that we can fix from errors we can not fix.
For example, we can not fix the most frequent "Uncaught ReferenceError: google is not defined" error, because it is caused by occasional browsers that do not work well with Google Maps API.
But we do not want to suppress that error either, because sometimes, by mistake, we may introduce problem in our own javascript that would generate the same error messages on mass scale for our users.

See: discussion in ivan-gandhi blog.

[identity profile] serjiojitser.livejournal.com 2016-02-07 03:21 pm (UTC)(link)
>For example, we can not fix the most frequent "Uncaught ReferenceError: google is not defined"

а можно её обвернуть в try catch с выдачей № браузера?

[identity profile] occam-aga.livejournal.com 2016-02-07 04:10 pm (UTC)(link)
а готовые решения для диагностики не рассматривались?

[identity profile] juan-gandhi.livejournal.com 2016-02-07 06:29 pm (UTC)(link)
A very good idea, I think.

[identity profile] ulrith.livejournal.com 2016-02-08 08:55 am (UTC)(link)
Interesting!
Why do you need to add a random value at the end of the error url?
Would you like to post server-side script which creates nice results table also? :)

[identity profile] ulrith.livejournal.com 2016-02-08 10:06 am (UTC)(link)
Oh thanks!
I'm going to parse the logs with general bash approach...

[identity profile] ulrith.livejournal.com 2016-02-08 10:14 am (UTC)(link)
Just to extract strings with errors from the logs to create simple text summary of the day in csv-file for my two fellow developers. :)

[identity profile] ulrith.livejournal.com 2016-02-08 12:05 pm (UTC)(link)
1. They're in the apache access log

2. Cron

[identity profile] ulrith.livejournal.com 2016-02-08 12:21 pm (UTC)(link)
Разумеется я добавил скрипт, иначе о чем вообще мы тут ведем речь? :))

[identity profile] ulrith.livejournal.com 2016-02-08 01:24 pm (UTC)(link)
Thank you for the approach and for the implementation example

[identity profile] ulrith.livejournal.com 2016-02-08 10:15 am (UTC)(link)
Also I'm currently thinking how to use Rollbar for better presentation...