mperdeck / jsnlog.js

Tiny JavaScript logging library, simple and well documented. Lots of options to filter logging data.
js.jsnlog.com
Other
130 stars 44 forks source link

Should handle un-caught rejected promises #35

Closed ghost closed 7 years ago

ghost commented 7 years ago

At the moment if an error is produced within a Promise then this won't be caught, as by default these aren't bubbled up to the onerror. onunhandledrejection should be also listened to such that these issues can be reported. e.g.

(function () {

    // Create onerror handler to log uncaught exceptions to the server side log, but only if there 
    // is no such handler already.
    // Must use "typeof window" here, because in NodeJs, window is not defined at all, so cannot refer to window in any way.
    if (typeof window !== 'undefined' && !window.onunhandledrejection) {
        window.onunhandledrejection = function (event) {

            // Send object with all data to server side log, using severity fatal, 
            // from logger "onerrorLogger"
            JL("onerrorLogger").fatalException({
                "msg": "unhandledrejection",
                "errorMsg": event.reason.message
            }, event.reason);

            // Tell browser to run its own error handler as well   
            return false;
        };
    }
})();
mperdeck commented 7 years ago

I've added this to JSNLog, and released it in version 2.22.0.