1- EACCES: This error happens when an attempt was made to access a file forbidden by the
operating system.
2- EADDRINUSE: This error happens when trying to bind an address to the server while
it’s already being used.
3- ENOENT: This error happens when trying to access a file or directory that does not exist.
User-specified errors
When designing an application, it is sometimes needed to have custom errors which only
have meaning in this specific application. A user-specified error can be as simple as throwing an
error provided only with a string. But it is a good practice to define a custom error using a class
extending JavaScript’s Error class.
class MyError extends Error {
A custom error object needs to have a property called message, which is going to be shown in
the error log. Since a custom error class extends the default Error class, it is needed to pass the
constructor arguments to the parent class’s constructor. Thus we need to use the super keyword.
class MyError extends Error {
constructor(message) {
super(message)
}
}
So far our custom error class is done. Its message gets set by the constructor of the Error class
so no further action is needed. Now it is possible to export this class and throw an error:
class MyError extends Error {
constructor(message) {
super(message)
}
}
[Link] = MyError
And here is the code to throw an error using this class:
const MyError = require('./MyError')
153
throw new MyError('It is brand new!')
The code above generates the output below:
throw new MyError('It is brand new!')
MyError: It is brand new!
at Object.<anonymous> (C:\Book samples\[Link]:7)
at Module._compile (internal/modules/cjs/[Link]:30)
at [Link]._extensions..js (internal/modules/cjs/[Link]:10)
at [Link] (internal/modules/cjs/[Link]:32)
at [Link]._load (internal/modules/cjs/[Link]:14)
at [Link] [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
If a class doesn’t extend the Error class it cannot be thrown:
class MyError {
constructor(message) {
[Link] = message
}
}
[Link] = MyError
If you try to throw an error using this class as below:
const MyError = require('./MyError')
throw new MyError('Not an error class!')
Here is the output [Link] will generate:
throw new MyError('Not an error class!')
154
^
MyError { message: 'Not an error class!' }
So it literally throws and object and not an error. Although this can terminate the program, the
identity of this object is not an error thus, there are no traces and no error message that [Link]
can understand.
A custom error can have any property wanted and can be accessed while catching that
exception, but here are the usual ones:
1- address
2- code
3- dest
4- errno
5- info
6- message
7- path
So far, you are familiar with the message property, and the rest of the properties are pretty much
self-explanatory.
Here is a custom error class with more properties:
class MyError extends Error {
constructor(message) {
super(message)
[Link] = 'MY_ERROR'
[Link] = 'You need to change something!'
}
}
[Link] = MyError
Throwing an error using this class generates the output below:
throw new MyError('Not an error class!')
155
MyError: Not an error class!
at Object.<anonymous> (C:\Book samples\[Link]:7)
at Module._compile (internal/modules/cjs/[Link]:30)
at [Link]._extensions..js (internal/modules/cjs/[Link]:10)
at [Link] (internal/modules/cjs/[Link]:32)
at [Link]._load (internal/modules/cjs/[Link]:14)
at [Link] [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
code: 'MY_ERROR',
info: 'You need to change something!'
And by catching this error it is possible to access each of the properties individually:
const MyError = require('./MyError')
try {
throw new MyError('Kaboom!')
} catch(e) {
[Link]([Link]) // Kaboom
[Link]([Link]) // MY_ERROR
[Link]([Link]) // You need to change something!
}
It is practical to include another property called name which holds the error class’s name. It is
possible to retrieve a class’s name by using [Link]:
class MyError extends Error {
constructor(message) {
super(message)
[Link] = [Link]
[Link] = 'MY_ERROR'
156
[Link] = 'You need to change something!'
}
}
[Link] = MyError
Now it is possible to see the error name:
const MyError = require('./MyError')
try {
throw new MyError('Kaboom!')
} catch(e) {
[Link]([Link]) // Kaboom
[Link]([Link]) // MyError
[Link]([Link]) // MY_ERROR
[Link]([Link]) // You need to change something!
}
If you don’t set the name of your error class, the default name will be Error since it is inherited
from the Error class.
AssertionError
AssertionError is a special kind of error thrown by assertions. An assertion is a strict way
of checking the truthy of values. If the condition is not met, it will throw an AssertionError, and
the program will terminate immediately. In order to use assertions, there is a built-in library
called assert, which needs to be imported.
const assert = require('assert')
This library provides us with a way to test the truthy of value just as below:
assert(7 > 2)
This value evaluates to true so nothing will happen, but if there is a falsely value, then an
AssertionError will be thrown:
assert(8 < 2)
This assert generates the error below:
[Link]
157
throw err;
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert(8 < 2)
at Object.<anonymous> (C:\Book samples\[Link]:1)
at Module._compile (internal/modules/cjs/[Link]:30)
at [Link]._extensions..js (internal/modules/cjs/[Link]:10)
at [Link] (internal/modules/cjs/[Link]:32)
at [Link]._load (internal/modules/cjs/[Link]:14)
at [Link] [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
The assert function can receive a custom message to be shown when the AssertionError is
thrown:
assert(8 < 2, 'Hmmm... Something seems wrong!')
158
This code throws an error generating the log below:
[Link]
throw err;
AssertionError [ERR_ASSERTION]: Hmmm... Something seems wrong!
at Object.<anonymous> (C:\Book samples\[Link]:1)
at Module._compile (internal/modules/cjs/[Link]:30)
at [Link]._extensions..js (internal/modules/cjs/[Link]:10)
at [Link] (internal/modules/cjs/[Link]:32)
at [Link]._load (internal/modules/cjs/[Link]:14)
at [Link] [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
As you can see, our error message is shown in the error log.
Assert library has more than only one function which they have different usage and are
really helpful. Some of them are explained below.
159
[Link](actual, expected[, message])
Checks the equality of the actual with the expected arguments deeply, meaning that the
children properties will be checked recursively.
[Link]({a: 7}, {b: 7})
This assert throws an error since the objects are not equal.
[Link]({n: [7, 8, 9]}, {n: [7, 8, 10]})
This assert will throw an error as well since the items in the array are not equal.
[Link](actual, expected[,
message])
Just as the opposite of the previous method, this methods checks for the inequality of the
actual with the expected arguments.
[Link]({n: [7, 8, 10]}, {n: [7, 8, 10]})
The code above throws an error since two arguments are deeply equal.
[Link](value)
Throws an error if the value passed to the method is not undefined or null.
[Link](null)
The code above won’t throw an error since the value is null, but the code below throws an error
since the value passed to it is a string.
[Link]('0')
This method is especially useful when testing the error argument in callbacks.
Error handling in callbacks
Callback functions by convention, have an argument specifically for errors, which is
places as the first argument. These kinds of errors won’t terminate the program unless specified
to do so. For instance, when reading a file, if the file doesn’t exist, the first argument will include
the error.; if not, it will be null.
160