JavaScript Node.js Basics Handling Errors in Node Handling the Error Event in Node

Peter Retvari
Peter Retvari
Full Stack JavaScript Techdegree Student 8,391 Points

request.on vs. try--catch


I would like to know is there any other way to make the error handling more user-friendly without the try {}? Maybe it's a stupid question, but can we handle the second type of error (missing protocol language) with a similar simple code, like we did in the first error (wrong URL)?

request.on('error', error => {

Because this kind of error (try) handling makes my code almost unreadable for me.

1 Answer

Tiago Fernandes
Tiago Fernandes
3,646 Points

Hi Peter

I found another solution that could help you.

const https = require('https');

https.get('', (res) => {

 if(res.statusCode !== 200){              //if not 200 then console.log
  console.log('Ooops! Something went wrong!')

regards Tiago