Last active
April 2, 2021 12:33
-
-
Save isocroft/a42a5a1ce887b2084679adcf86bf907f to your computer and use it in GitHub Desktop.
This is a polyfill that makes it possible to replicate the Firefox properties for browser errors to get the 'fileName', 'columnNumber' and 'lineNumber'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof InstallTrigger === "undefined") { // If the browser isn't a Gecko-based browser (e.g. Firefox) | |
Object.defineProperty(Error.prototype, 'columnNumber', { | |
configurable: false, | |
enumerable: false, | |
get: function () { | |
const stackString = (this.stack || '').toString(); | |
const regexStackFrameLine = /at (?:(?:[\w ])+) \(([^:]+)\:([\d]+)\:([\d]+)\)\s*/g | |
const [ _, fileName, lineNumber, columnNumber ] = regexStackFrameLine.exec(stackString) || [ '', '<anonymous>', '1', '1' ] | |
return (typeof(chrome) !== 'undefined' || typeof(msCrypto) !== 'undefined' || typeof(process) !== 'undefined') ? columnNumber : 0; | |
} | |
}); | |
Object.defineProperty(Error.prototype, 'lineNumber', { | |
configurable: false, | |
enumerable: false, | |
get: function () { | |
const stackString = (this.stack || '').toString(); | |
const regexStackFrameLine = /at (?:(?:[\w ])+) \(([^:]+)\:([\d]+)\:([\d]+)\)\s*/g | |
const [ _, fileName, lineNumber, columnNumber ] = regexStackFrameLine.exec(stackString) || [ '', '<anonymous>', '1', '1' ] | |
return (typeof(chrome) !== 'undefined' || typeof(msCrypto) !== 'undefined' || typeof(process) !== 'undefined') ? lineNumber : this.line.number; | |
} | |
}); | |
Object.defineProperty(Error.prototype, 'fileName', { | |
configurable: false, | |
enumerable: false, | |
get: function () { | |
const stackString = (this.stack || '').toString(); | |
const regexStackFrameLine = /at (?:(?:[\w ])+) \(([^:]+)\:([\d]+)\:([\d]+)\)\s*/g | |
const [ _, fileName, lineNumber, columnNumber ] = regexStackFrameLine.exec(stackString) || [ '', '<anonymous>', '1', '1' ] | |
return (typeof(chrome) !== 'undefined' || typeof(msCrypto) !== 'undefined' || typeof(process) !== 'undefined') ? fileName : this.sourceURL; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh so you can use it like so ? Cool 💯 🥇
I feel this can also be used for V8. Like NodeJS right ?