gonzales-pe icon indicating copy to clipboard operation
gonzales-pe copied to clipboard

ParseError: Stack and property improvements

Open shellscape opened this issue 9 years ago • 2 comments

ParseError is an improvement upon the previous error reporting. But it causes some data loss and important information during conversion from Error to ParseError. The following code is offered as an improvement to the object, adding a track trace (important!) and attaching existing/conflicting property names from Error to a super property:

/**
 * @param {Error} e
 * @param {String} css
 */
function ParsingError(e, css) {
  this.line = e.line;
  this.syntax = e.syntax;
  this.css_ = css;
  this.super_ = {};

  for (var prop in e) {
    if (e.stack) {
      this.stack = e.stack;
    }
    else if (e.hasOwnProperty(prop)) {
      (this[prop] ? this.super_ : this)[prop] = e[prop];
    }
  }

  if (e.stack) {
    this.stack = e.stack;
  }
  else if (Error.hasOwnProperty('captureStackTrace')) {
    e.captureStackTrace(this, this.constructor);
  }
  else {
    Object.defineProperty(this, 'stack', {
      value: new Error().stack
    });
  }
}

shellscape avatar Nov 03 '15 18:11 shellscape

What information except for stack trace you want to get from e?

tonyganch avatar Nov 04 '15 07:11 tonyganch

That's all I needed at the moment. The code allows for expansion if we modify errors to contain anything other than line and syntax. It's just forward-thinking.

shellscape avatar Nov 04 '15 12:11 shellscape