Loading topic
Error. Before you extend it, you need to know which properties an Error object has, where each one lives, and which ones the language standard actually guarantees. This chapter covers the three properties you use all the4 min readthrow and try/catch, and how to run code in Node.js or a browser console. You should also know the name, message, and stack properties from the previous chapter.5 min readclass, extends, super), how throw and try/catch work, and how to run a script in Node.js or the browser console. It also builds on the previous chapters' description of the name, message, and stack properties.5 min readError and passes message and options to super, as shown in the previous chapter. It also assumes you are comfortable with ES2015 classes, property descriptors, and running snippets in Node.js or a browser console.5 min readError, pass message and options to super, and set this.name. It also assumes you know ES2015 class syntax, try/catch, and how to run code in Node.js or a browser console.5 min readcatch block receives every error thrown inside its try, whatever the reason. Real code usually wants to handle some errors and let the rest propagate. A missing record might become a warning, while a broken database connection should still crash the request. This chapter shows how to us5 min readclass ... extends Error, set its name, and catch errors with try/catch and instanceof. It covers the cause option that ES2022 added to the Error constructor. You will see how to pass it, how custom classes must forward it, and 4 min readname and structured fields, how to tell errors apart with instanceof, and how the cause option attaches one error to another. It also uses async/await and Node.js ES modules.5 min read{ cause: err } to the Error constructor and how to wrap errors at layer boundaries. It also uses generator functions (function* and yield). You only need to know that a generator produces values one at a time and works with for...of.5 min read{ cause } to the Error constructor and how to wrap errors at layer boundaries, both covered in earlier chapters. You should also be comfortable with property descriptors (Object.getOwnPropertyDescriptor) and the difference between own and inher5 min readAggregateError is a built-in error type (added in ES2021) that holds a list of errors in one object.4 min readname and structured fields, how the cause option works, and how to walk a cause chain. It also assumes you know that cause is an own, non-enumerable property. Here those pieces come together on a practical problem: tu5 min readclass ... extends Error, set its name, and pass options through to super. You should also be able to run snippets in Node.js, because most examples print V8-specific output. Node.js runs on V8, and so do Chrome and Edge.5 min readinstanceof to tell errors apart in catch blocks. That works in any modern engine running native class syntax. It can stop working without any warning when a build tool compiles your classes down to ES5. ES5 is the JavaScript version from before class existed. This c6 min readclass extends Error, set its name, add fields such as status, and check errors with instanceof in a catch block. It also assumes you know how to wrap an error with the cause option. Earlier chapters covered each of these. Here 5 min read