chapter seven

7 Errors

 

This chapter covers

  • How errors serve recovery and debugging needs
  • Why stack traces cost memory and performance
  • Placing error handlers at semantic boundaries
  • When async operations break error propagation
  • Leveraging global error handlers as a last resort

JavaScript errors are more complicated than they seem.

So far throughout this book, we’ve built up JavaScript’s execution model piece by piece. We’ve looked at how values live in memory, how scopes control access, how objects organize data, and how functions operate on data. Each layer adds complexity and each complexity adds a new way for things to go wrong. When they do go wrong, JavaScript’s error system takes over. However, most developers are surprised to find just how little the language specification has to say about errors! What developers actually encounter when their code fails is typically an intricate web of engine and runtime-specific behaviors and historical accidents.

7.1 The dual, and often opposing purpose of errors

7.2 What are JavaScript errors?

7.2.1 The native errors

7.2.2 Modern error types

7.2.3 The error cause chain

7.3 Node.js errors

7.4 The error stack

7.4.1 Stacks are expensive

7.4.2 Mitigating the costs

7.5 Error propagation and control flow

7.6 Summary