chrisblakley / Nebula

Nebula is a WordPress theme framework that focuses on enhancing development. The core features of Nebula make it a powerful tool for designing, developing, and analyzing WordPress websites consistently, yet its deliberately uncomplicated code syntax also serves as a learning resource for programmers themselves.
https://nebula.gearside.com
GNU General Public License v2.0
143 stars 36 forks source link

Add line numbers to JS catch statements if/when it becomes standardized [❌ no proposal at this time] #2206

Open chrisblakley opened 2 years ago

chrisblakley commented 2 years ago

Currently only Firefox supports error.lineNumber inside of catch() statements, so we can't use it in Nebula. Monitor it so that if/when it becomes standard in all browsers that we can use it in all of Nebula's catches.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/lineNumber

try {
    //do something
} catch(error){
    console.log('Line Number:', error.lineNumber); //Only currently works in Firefox
}
chrisblakley commented 1 year ago

This tidbit from ChatGPT was particularly insightful– might be a nice stopgap until lineNumber is available in all browsers:


The lineNumber property of a JavaScript Error object, which is available inside the catch() block, is not a standard part of the ECMAScript specification. It is supported by some modern browsers, including Firefox, but not by others. This is why the lineNumber property is only available in Firefox and not in other browsers.

To get the line number of an error in JavaScript, you can use the error.stack property, which contains a stack trace of the error. The stack trace includes the line number where the error occurred. Here is an example:

try {
  // Do something that might throw an error.
} catch (error) {
  // Get the line number of the error from the stack trace.
  let lineNumber = error.stack.split('\n')[1].split(':')[1];

  // Output the error message and line number.
  console.log(error.message + ' (line ' + lineNumber + ')');
}

This code gets the stack trace of the error and then extracts the line number from the second line of the stack trace. It then outputs the error message and line number. Note that this method is not supported by all browsers and may not work in all cases.