javascript - How can I automatically log warnings and errors from the console in Electron? -
i'd log these automatically generated errors , warnings in application can debug issues users have, far i've not been able find solution logging these. know of package or method?
thank you.
the basic idea listen errors subscribing window.onerror
:
window.onerror = function (msg, url, lineno, columnno, error) { // log server return false; }
but browsers don't provide stacktrace, have wrap functions calls @ top of stack chain try
catch
:
function wraperrors(fn) { // don't wrap function more once if (!fn.__wrapped__) { fn.__wrapped__ = function () { try { return fn.apply(this, arguments); } catch (e) { logerrortoyourserver(e); // report error throw e; // re-throw error } }; } return fn.__wrapped__; } var invoke = wraperrors(function(obj, method, args) { return obj[method].apply(this, args); }); invoke(math, 'highest', [1, 2]); // no method math.highest
this function has called:
- at start of application (e.g. in $(document).ready if use jquery)
- in event handlers, e.g. addeventlistener or $.fn.click
- timer-based callbacks, e.g. settimeout or requestanimationframe
for example:
$(wraperrors(function () { // application start dosynchronousstuff1(); // doesn't need wrapped settimeout(wraperrors(function () { dosynchronousstuff2(); // doesn't need wrapped }); $('.foo').click(wraperrors(function () { dosynchronousstuff3(); // doesn't need wrapped }); }));
(code shamelessly copied sentry's blog post)
you can find more information in following links:
- globaleventhandlers.onerror
- sentry's blog post
- this gist on how overwrite addeventlistener
there commercial products
Comments
Post a Comment