Throwing meaningful errors
When an error occurs, it is important that it is meaningful. This means that the error should contain enough information to understand what happened and, potentially, how to fix it.
The error object
The error object is an instance of the Error
class. This class has a constructor that accepts a message as a parameter. This message will be used to describe the error. Here is an example:
const myError = new Error('This is an error message') throw myError
Here is the output of the previous code:
file:///file.js:1 const myError = new Error('This is an error message') ^ Error: This is an error message at file:///file.js:1:17 at ModuleJob.run (node:internal/modules/esm/module_job:192:25) at async DefaultModuleLoader.import (node:internal/modules/esm/loader:228:24...