Errors
The message
property of the Error
object has the value that you pass to the Error()
constructor. The stack
property contains a string describing the stack trace of the JavaScript call stack at the moment the Error
object was created (but not where the throw
statement throws it).
The subclasses of the Error
class include EvalError
, RangeError
, ReferenceError
, SyntaxError
, TypeError
, and URIError
. The name
property of all Error
classes is the same as the constructor name.
You can define your own Error
subclasses that best encapsulate the error conditions of your program.
class HTTPError extends Error {
constructor(status, statusText, url) {
super(`${status} ${statusText}: ${url}`);
this.status = status;
this.statusText = statusText;
this.url = url;
}
get name() { return 'HTTPError'; }
}
let error = new HTTPError(404, 'Not Found', 'http://example.com/');
error.status; // => 404
error.message; // => '404 Not Found: http://example.com/'
error.name; // => 'HTTPError'