Skip to content

Instantly share code, notes, and snippets.

@csnover
Last active August 29, 2015 14:06
Show Gist options
  • Save csnover/5900a60070d9a40fc7d8 to your computer and use it in GitHub Desktop.
Save csnover/5900a60070d9a40fc7d8 to your computer and use it in GitHub Desktop.
What should the first line of an error stack trace be, in different scenarios?
// scenario 1
var error = new Error('foo');
error.stack; // first line = ?
// scenario 2
var error = new Error('foo');
error.message = 'bar';
error.stack; // first line = ?
// scenario 3
var error = new Error('foo');
error.stack;
error.message = 'bar';
error.stack; // first line = ?
// scenario 4
var error = new Error('foo');
error.message = 'bar';
try {
throw error;
}
catch (error) {
error.stack; // first line = ?
}
// scenario 5
var error = new Error('foo');
try {
throw error;
}
catch (error) {
error.message = 'bar';
error.stack; // first line = ?
}
// scenario 6
function BarError() {
Error.apply(this, arguments);
this.message = 'bar';
}
BarError.prototype = new Error();
BarError.prototype.name = 'BarError';
BarError.prototype.constructor = BarError;
var error = new BarError();
try {
throw error;
}
catch (error) {
error.stack; // first line = ?
}
// scenario 7
function BarError() {
Error.apply(this, arguments);
this.message = 'bar';
}
BarError.prototype = Object.create(Error.prototype);
BarError.prototype.name = 'BarError';
BarError.prototype.constructor = BarError;
var error = new BarError();
try {
throw error;
}
catch (error) {
error.stack; // first line = ?
}
@csnover
Copy link
Author

csnover commented Sep 13, 2014

IE 11—

  1. undefined
  2. undefined
  3. undefined
  4. Error: bar
  5. Error: bar
  6. BarError: bar
  7. BarError: bar

Old V8 (Node.js 0.10)—

  1. Error: foo
  2. Error: bar
  3. Error: foo
  4. Error: bar
  5. Error: bar
  6. BarError
  7. undefined

New V8 (Node.js master, Chrome 36, Opera 24)—

  1. Error: foo
  2. Error: foo
  3. Error: foo
  4. Error: foo
  5. Error: foo
  6. Error
  7. undefined

Firefox 32—

  1. first frame of stack
  2. first frame of stack
  3. first frame of stack
  4. first frame of stack
  5. first frame of stack
  6. first frame of stack
  7. empty string

Safari 7.0.6—

  1. undefined
  2. undefined
  3. undefined
  4. first frame of stack
  5. first frame of stack
  6. first frame of stack
  7. first frame of stack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment