tonyganch / gonzales-pe

CSS parser with support of preprocessors
MIT License
330 stars 67 forks source link

ParseError: Stack and property improvements #133

Open shellscape opened 9 years ago

shellscape commented 9 years ago

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
    });
  }
}
tonyganch commented 9 years ago

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

shellscape commented 9 years ago

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.