Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics 2017 Handling Errors in Node Handling Status Code Errors

tal Shnitzer
PLUS
tal Shnitzer
Courses Plus Student 5,242 Points

adding friendlier error messages?

in the end of this video the instructor suggests that we will improve it by adding friendlier error messages. is there someone who have accomplished this task and can share it? or explain what does it mean?

2 Answers

Chris Pulver
Chris Pulver
7,624 Points

His suggestion is to make the errors more user friendly so when they display, a user would have a better understanding of what went wrong. I don't remember the lesson fully, but I think you have the opportunity to add more nuanced errors. Below is one that I pulled out of my project:

request.on('error', error => {
      if(error.message.includes("getaddrinfo")) {
        console.error("Problem with request. Please check the URL.");
      } else {
          console.error(`Problem with request: ${error.message}`);
        }
    });

If I remember correctly, his code just returned something about "getaddrinfo" and "enotfound", which doesn't make a lot of sense.

I had a go at this question and added an else if statement so that each error code could potentially have it's own unique text. However i think it might be better to have the error text stored as a variable that can be called as and when that error occurs. This way, none devs could change the error text without editing the code. I also don't know if this is the best method of improving error messaging

        });
                            } else if (response.statusCode === 404) {

                              const message = `There was an error getting the profile for ${username} (${http.STATUS_CODES[response.statusCode]}) - this means I just improved the error messaging`;
                                const statusCodeError = new Error(message);
                              printError(statusCodeError);

                            }
                              else {
                                const message = `There was an error getting the profile for ${username} (${http.STATUS_CODES[response.statusCode]})`;
                                const statusCodeError = new Error(message);
                              printError(statusCodeError);

                            }});