import { onError } from '../track'; export default function () { // Check if the browser supports window.error if (typeof window.onerror === 'undefined' && window.console) { // Browser doesn't support window.error, log errors via console instead let oldFnType = window.console.error; window.console.error = function () { onError({ data: Array.prototype.slice.call(arguments).join(' '), occurredAt: new Date(), source: 'Console', stack: 'Not Available', category: 'Not Available' }); oldFnType.apply(this, arguments); }; // window.onerror is not supported by the browser // Track errors by checking the console instead } else { window.onerror = function (msg, source, lineNo, columnNo, error) { let target, data; let stack = "Unavailable"; let category = "Unavailable"; // Some (older) browsers don't give the error object parameter if (error != null) { target = source + " - Line: " + lineNo + " - Column: " + columnNo; data = error.name + ": " + error.message; category = error.name; // error.stack is currently marked as non-standard if (error.stack != null) { stack = error.stack; } // Some (older) browsers don't give the columnNo parameter } else if (columnNo != null) { target = source + " - Line: " + lineNo + " - Column: " + columnNo; data = msg; } else { target = source + " - Line: " + lineNo; data = msg; } onError({ data: data, occurredAt: new Date(), source: target, stack: stack, category: category }); } } }