Preface
There are many types of errors that may occur in Node.js applications. For example: When a syntax error or runtime error occurs, a JavaScript error will be triggered; when trying to access a file that does not exist or is not accessed, a system error will be triggered; in addition to JavaScript errors and system error errors, users can also customize the errors.
1. Misclassification
Node.js is a JavaScript runtime platform, and its application errors are all an Error instance or Error subclass instance.
In Node.js applications, there are four possible errors:
1. Standard JavaScript errors, such as:
<EvalError> : Thrown when eval() calls fail
<SyntaxError> : Thrown when using illegal JavaScript syntax
<RangeError> : Thrown when a value is not within the specified range
<ReferenceError> : Thrown when undefined variable is used
<TypeError> : When passing an error type parameter
<URIError> : Thrown when a global URI function is used incorrectly
2. System error. This type of error is triggered by the underlying system, such as trying to open a non-existent file, trying to send data through a closed soket, etc.
3. Custom errors, such errors are triggered by the user in the application code.
4. Assertion error. When the code violates legal logic, this type of error is triggered by the Node.js assert module.
2. JavaScript Errors and System Errors
2.1 JavaScript Errors and Custom Errors
Standard JavaScript errors are provided by the JavaScript language, indicating syntax errors or improper use of APIs. All errors are an instance of Error class, while standard JavaScript errors are built by the language itself when an error occurs.
When customizing errors, use the constructor to create an instance:
> throw new Error('An error occurred, this is the error message');Error: An error message was found at repl:1:7 at REPLServer.defaultEval (repl.js:248:27) at bound (domain.js:280:14) at REPLServer.runBound [as eval] (domain.js:293:12) at REPLServer.<anonymous> (repl.js:412:12) at emitOne (events.js:82:20) at REPLServer.emit (events.js:169:7) at REPLServer.Interface._onLine (readline.js:210:10) at REPLServer.Interface._line (readline.js:549:8) at REPLServer.Interface._ttyWrite (readline.js:826:14) As above, we customized an error and threw the error using the throw keyword. In this error object, it includes the error message and the error stack information stack . After exception capture, this information can be accessed through the following two properties:
1. error.message - Error message
2. error.stack - Error stack trace information
In addition to the properties of the Error instance itself, you can also customize some error properties. For example, customize a property status that represents the status:
var error = new Error('The page you visited does not exist');error.status = 404;2.2 System Error
System errors are an extension of JavaScript error Error objects, which represent operational errors that programs can handle, and these error messages are generated at the system level. In addition to the attributes in the Error instance, the system error instance also includes the following attributes:
1. error.syscall - a string representing the failed system call information
2. error.errno - an integer error code
3. error.code - represents an error string, usually starting with capital letter E
3. Exception capture
Node.js adopts event-driven and asynchronous programming basis, which means that try / catch cannot catch errors that occur when processing asynchronously. For this case, we can use the following two ways to catch or pass the error:
Exception capture in Node.js callback function
Node.js has many asynchronous APIs, which are subsequently processed through the callback function callback. If an error occurs, an Error object will usually be included in the first parameter of the callback function; if no error occurs, the first parameter will be null . like:
const fs = require('fs');fs.readFile('A non-existent file', (err, data) => { if (err) { console.error('Read file error', err); return; } // Other processing});Event-based error handling
If the object is an EventEmitter , the error can be captured and handled through the的'error' the object:
const net = require('net');const connection = net.connect('localhost');// Add an 'error' event handler to the stream:connection.on('error', (err) => { // If the connection is reset by the server or cannot connect to the specified server// or other connection errors, the error will be passed here to console.error(err);});connection.pipe(process.stdout);Summarize
The above is a summary of some error types in Node.js. If you can understand the error types in Node.js, you can help us better handle errors and exception catches. I hope the content of this article will be helpful to everyone.