Summarize some error types in Node. js and summarize the node. js types.

Source: Internet
Author: User
Tags uppercase letter

Summarize some error types in Node. js and summarize the node. js types.

Preface

Multiple types of errors may occur in Node. js applications. For example, if a syntax error or runtime error occurs, a JavaScript error is triggered. If you try to access a file that does not exist or is not accessed, a system error is triggered; in addition to JavaScript errors and system errors, you can also customize errors.

1. Error category

Node. js is a JavaScript runtime platform, and its application errors are allErrorInstance orErrorSubclass instance.

In Node. js applications, the following four types of errors may occur:

1. Standard JavaScript errors, such:

<EvalError> : When you calleval() Throw upon failure

<SyntaxError>: Thrown when invalid JavaScript syntax is used

<RangeError> : Throw when a value is not within the specified range.

<ReferenceError> : Throw when undefined variables are used

<TypeError>: Thrown when an error type parameter is passed

<URIError> : Thrown when a global URI function is used incorrectly

2. system errors. These errors are triggered by the underlying system, such as when attempting to open a non-existent file or sending data through a disabled Soket.

3. Custom errors. These errors are triggered by the user in the application code.

4. assertion errors. When the Code violates the legal logicassertModule trigger

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 areErrorClass instances, and standard JavaScript errors are built by the language itself in the case of errors.

When a custom error occurs, use the constructor to create an instance:

> Throw new Error ('error, This is the Error information'); Error: Error, this is the Error message 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 shown above, we have customized an error and throw the error using the throw keyword. This error object contains the error message.messageAnd incorrect stack informationstackAfter exception capture, you can access this information through the following two attributes:

1,error.message-Error message

2,error.stack -Error stack tracing information

In addition to the attributes of the Error instance, you can also customize some Error attributes. For example, to customize an attribute status that indicates the status:

Var error = new Error ('the page you accessed does not exist'); error. status = 404;

2.2 System Error

System Error: JavaScript ErrorErrorAn extension of the object, which indicates the operation errors that the program can handle. These error messages are generated at the system level. In a system error instanceErrorIn addition to the attributes in the instance, the following attributes are also included:

1,error.syscall -A string indicating failed system call Information

2,error.errno-Error code of an integer

3,error.code -Indicates an error string, usually starting with an uppercase letter E.

3. Exception capture

Node. js adopts the event-driven and asynchronous programming basis, which meanstry / catchErrors Caused by asynchronous processing cannot be captured. In this case, we can use the following two methods to capture or pass errors:

Exception capture in Node. js callback Functions

Node. js has many asynchronous APIs, which are processed by the callback function callback. If an error occurscallbackThe first parameter of the function containsErrorObject. If no error occurs, the first parameter isnull. For example:

Const fs = require ('fs'); fs. readFile ('a nonexistent file', (err, data) =>{ if (err) {console. error ('file read error', err); return;} // other processing });

Event-based error handling

If the object isEventEmitter, Through the object'Error'Event capture and handling errors:

Const net = require ('net'); const connection = net. connect ('localhost'); // Add an 'error' event processor for stream: connection. on ('error', (err) =>{// if the connection is reset by the server or the specified server cannot be connected // or other connection errors occur, the error will be passed to the console. error (err) ;}); connection. pipe (process. stdout );

Summary

The above is a summary of some error types in Node. js. Understanding the error types in Node. js can help us better handle errors and capture exceptions. I hope this article will help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.