The Node.js error class hierarchy consists of about half a dozen unique classes, most of which inherit from the baseline Error
class. A Node.js application will typically experience errors that fall into one of four categories:
RangeError
, ReferenceError
, and TypeError
.One important distinction between Node.js and standard JavaScript is that all exceptions thrown by Node.js are instances of the Error
class. This allows Node.js to associate the stack trace and other valuable metadata with every error that occurs within a Node.js application.
Here is the full Node.js error class hierarchy:
Beyond the Error
class and its children, starting with version 8+ Node.js now associates an error code
property with each error that is thrown. These codes are simply string constants, but their existence allows code to check for a specific error type without having to worry about the messiness associated with checking the actual error message
property string.
For example, in the past performing logic based on a specific caught error type involved something like this:
try {
// Node.js code generating an Error.
} catch (error) {
if (error.message === 'Uh oh, an error has occurred') {
// Perform some logic.
}
}
Comparing strings is a rather messy affair, particularly when internationalization gets involved with various locales. Thus, Node.js error codes
were introduced and allow the error.code
property to be checked against a standard string constant associated with the type of error you're checking for. Thus, the code above using error codes looks something like this:
try {
// Node.js code generating an Error.
} catch (error) {
if (error.message === 'ERR_HTTP_INVALID_STATUS_CODE') {
// Perform some logic.
}
}
The full list of Node.js error codes can be found below:
As we publish future error-specific articles in this series we'll update the full list above with relevant tutorial and article links for each error class and error code, so this post can act as a go-to resource for Node.js error handling tips.
Let's briefly look at each top-level error class type found in Node.js. These top-level errors will server as a basis for exploring specific errors and error codes in future articles.
The Error
class is a generic JavaScript Error
object that doesn't include any information about why this error occurred. However, since these are Node.js-generated Errors
, each Error
instance includes a stack trace, error code (if applicable), and any other relevant metadata. This metadata is provided via a handful of core Error
class properties:
error.code
- A string constant indicating the specific error type.error.message
- A string that describes the error.error.stack
- A string that describes where in the code the Error
occurred.As we saw above, since all errors in Node.js inherit from the Error
class, all errors will include these baseline properties.
Indicates an assertion failure using the assert
module.
Indicates that an argument was not within a valid set or range of values. Node.js generates and throws RangeErrors
immediately when invalid arguments are detected.
Indicates that an undefined variable was accessed.
Indicates that a portion of the code is not valid JavaScript. Typically, SyntaxErrors
will only occur as a result of code evaluation techniques, such as using the Function()
or eval()
functions.
Indicates that an argument was passed to a function that expected an argument of a different type. Similar to the RangeError
, Node.js will generate and throw TypeErrors
immediately when an invalid argument type is detected.
As discussed in the introduction, one of the four common categories in which Node.js can experience errors is when something at the system level goes wrong. In such cases, a System Error
is generated. These may occur due to improper IO operations, invalid permissions, and so forth.
In addition to the error.code
property found in the inherited Error
class, System Errors
also include a few extra properties that are relevant to system-level problems:
error.errno
- A number or string value indicating the error number associated with the particular system-level error.code
.error.syscall
- A string that describes the syscall
that failed.error.address
- An optional string that describes the remote address for which the connection-related error occurred.error.port
- An optional number that describes the remote port for which the connection-related error occurred.There are far too many possible System Error
codes that can occur, but feel free to look at the man page documentation for more information.
That's just a small taste of overall Node.js error class hierarchy. Stay tuned for more in-depth articles examining each of these error in greater detail, and be sure to check out Airbrake's robust error monitoring software, which provides real-time error monitoring and automatic exception reporting for all your development projects. Airbrake's state of the art web dashboard ensures you receive round-the-clock status updates on your application's health and error rates. No matter what you're working on, Airbrake easily integrates with all the most popular languages and frameworks. Plus, Airbrake makes it easy to customize exception parameters, while giving you complete control of the active error filter system, so you only gather the errors that matter most.
Check out Airbrake's error monitoring software today and sign up for a 14-day free trial. See for yourself why so many of the world's best engineering teams use Airbrake to revolutionize their exception handling practices!