Sub topic 1
What an Error Object Contains
Every custom error class in this tutorial builds on the plain Error object. This chapter covers the three properties you will rely on most, name, message, and stack, and shows where each one lives on the object.
Prerequisites
This chapter assumes you are comfortable with:
- JavaScript syntax and functions
throwandtry/catch/finally- Running code in the browser console or Node.js
ES2015 classes and async/await come up in later chapters. You don't need them here.
The three core properties
When you call new Error('Disk full'), you get an object with three properties that matter:
| Property | What it holds | Where it lives |
|---|---|---|
| `name` | The error type, `'Error'` by default | Inherited from `Error.prototype` |
| `message` | The text you passed to the constructor | Own property, only if you passed a message |
| `stack` | A text trace of where the error was created | Own property in most engines; not standardized |
const err = new Error('Disk full');
console.log(err.name); // 'Error'
console.log(err.message); // 'Disk full'
console.log(typeof err.stack); // 'string'
console.log(String(err)); // 'Error: Disk full'
console.log(Object.hasOwn(err, 'name')); // false
console.log(Object.hasOwn(err, 'message')); // true
console.log(Object.keys(err)); // []Object.keys returns an empty array because message and stack are non-enumerable. A later chapter shows why this matters for logging.
How toString builds its output
Error.prototype.toString joins name and message with a colon and a space. If message is empty, it returns only the name. That is why String(new Error()) prints 'Error'. Because name comes from the prototype, a subclass that never sets its own name still prints as Error. The chapter on naming errors deals with this.
The stack property
stack is not part of the ECMAScript standard, but every major engine supports it. Its format differs between engines:
- V8 (Node.js, Chrome) starts the stack with the
toStringresult, followed by lines likeat loadUser (app.js:12:9). - Firefox and Safari leave out the header line and list frames as
[email protected]:12:9.
The trace is recorded when the error is created. Where it is thrown has no effect. If you build an error in one function and throw it from another, the stack points to the first function.
Common mistakes
- Throwing a string.
throw 'not found'is allowed, but the value has nonameand nostack, so you lose the location of the failure. Always throwErrorobjects. - Assuming
messageis always set.new Error()has no ownmessage. Reading it returns the inherited empty string. - Expecting
JSON.stringify(err)to show the message. It returns'{}'because the useful properties are non-enumerable.
Exercise
What do String(new Error()) and Object.hasOwn(new Error(), 'message') return, and why?
Show answer
String(new Error()) returns 'Error', because toString leaves out the colon and message when the message is empty. Object.hasOwn(new Error(), 'message') returns false, because the constructor creates an own message only when you pass a message argument. Reading message falls back to the empty string on Error.prototype.
Exercise
A function creates const e = new Error('bad input') and passes it to a helper that throws it. Which function does the stack trace point to?
Show answer
It points to the function that called new Error, because the stack is captured when the error is created, not when it is thrown.