When working with the HTTPS module in Node.js, it’s potential to come across an error when the web connection is misplaced or disrupted whereas the HTTPS request is being made. This will trigger the request to fail and throw an error, disrupting the traditional move of the applying.
Contemplate the next code instance, which makes an HTTPS request to a distant server utilizing the HTTPS module:
Javascript
|
|
If the web connection is misplaced or disrupted whereas the HTTPS request is being made, the next error could also be thrown.
Error
This error signifies that the connection to the server was refused, seemingly attributable to an issue with the web connection.
Strategy: To deal with this error, we will use the error occasion of the https.ClientRequest object returned by the https.get() operate. The error occasion is emitted when an error happens whereas making the request. We are able to connect a listener operate to this occasion to deal with the error.
Instance 1: Right here is an instance of how you can retry the HTTPS request utilizing a loop. On this code, the makeRequest() operate is outlined to make an HTTPS request to the URL https://www.instance.com utilizing the https.get() operate. If the request is profitable, the standing code of the response is logged to the console. If the request fails, the error occasion of the https.ClientRequest object is emitted, and the error is logged to the console. The code then checks the error code of the error object. If the error code is ECONNREFUSED, which signifies that the connection was refused, the code enters a loop that retries the request a most of 5 occasions. The variety of retries is tracked utilizing the numRetries variable, and a message is logged to the console indicating the present try and the whole variety of makes an attempt. After the utmost variety of retries has been reached, the loop exits, and the request just isn’t retried additional. If the error code just isn’t ECONNREFUSED, the request just isn’t retried.
Javascript
|
|
Output: This code will retry the HTTPS request as much as 5 occasions if the error was attributable to an issue with the web connection. The output of this code might look one thing like this:
modified(retry)
Instance 2: On this code instance, the retry loop is applied utilizing an if assertion that checks if the variety of retries is lower than the utmost variety of retries allowed. If the situation is true, the makeRequest() operate known as once more with numRetries incremented by 1. This continues till the utmost variety of retries is reached, at which level the if assertion evaluates to false and the loop is exited. This code may even retry the HTTPS request as much as 5 occasions if the error was attributable to an issue with the web connection. The output of this code will probably be much like the earlier instance.
Javascript
|
|
Output:
Instance 3: One method that’s typically used to deal with errors when making HTTPS requests in Node.js is to make use of a library similar to axios or request-promise. These libraries present built-in error dealing with and retry performance, which might make it simpler to implement error dealing with in your software. For instance, utilizing axios, you may make an HTTPS request and deal with errors as follows:
On this code, the axios.get() operate is used to make an HTTPS request to the URL https://www.instance.com. The then() methodology is used to specify a callback operate that’s executed if the request is profitable, and the catch() methodology is used to specify a callback operate that’s executed if the request fails.
If the request is profitable, the standing code of the response is logged to the console. If the request fails, the error is logged to the console and the error code is checked. If the error code is ECONNREFUSED, which signifies that the connection was refused, the axios.get() operate known as once more to retry the request. If the request is profitable this time, the standing code is logged to the console. If the request nonetheless fails, the error is logged to the console once more.
Javascript
|
|
Output: If the request fails with a connection refused error, the error is logged to the console and the request is retried:
axios dealing with output
The axios library supplies a handy solution to make HTTP requests in Node.js and contains options like automated retries and error dealing with. On this code, the catch() methodology is used to deal with errors that will happen whereas making the request, and the then() methodology is used to execute a callback operate if the request is profitable. Utilizing a library similar to axios or request-promise could be a extra handy and sturdy method to dealing with errors when making HTTPS requests in Node.js, because it supplies built-in error dealing with and retry performance.
You will need to word that these examples solely deal with the error when the web connection is misplaced or disrupted. It is usually essential to deal with different forms of errors that will happen, similar to invalid URLs or server-side errors. Dealing with errors when working with the HTTPS module in Node.js is essential to make sure that the applying continues to operate correctly. By including error dealing with logic, similar to retrying the request when the error is because of an issue with the web connection, it’s potential to stop disruptions within the regular move of the applying.
Reference: https://github.com/axios/axios