1 00:00:00,061 --> 00:00:04,041 To keep our code DRY, let's add a function that prints error messages. 2 00:00:05,447 --> 00:00:08,459 I've added a snippet in the teacher's notes, so you can just copy and paste. 3 00:00:12,325 --> 00:00:14,767 We can call this function in all of our handlers and 4 00:00:14,767 --> 00:00:17,283 replace the statements that are currently there. 5 00:00:19,935 --> 00:00:21,280 So in our try block. 6 00:00:24,318 --> 00:00:25,713 PrintError. 7 00:00:29,516 --> 00:00:34,317 Save, Same here in our on method. 8 00:00:57,630 --> 00:01:00,312 And we'll test this out by running the code again. 9 00:01:00,312 --> 00:01:03,801 We just get our error message, great. 10 00:01:03,801 --> 00:01:08,418 HTTP provides us with status codes that indicate the status of a request. 11 00:01:08,418 --> 00:01:12,419 When a request is completed without issue, we'll get a status of 200. 12 00:01:12,419 --> 00:01:15,655 Maybe there was a server error, which will give us a 500 error. 13 00:01:18,453 --> 00:01:22,103 Maybe the endpoint moved, and we'll get 301. 14 00:01:22,103 --> 00:01:25,770 If the endpoint is incorrect, we'll get a status code of 404 Not Found. 15 00:01:28,030 --> 00:01:32,260 So far, we've assumed that each API response will be okay. 16 00:01:32,260 --> 00:01:35,300 Let's handle all the status codes that aren't 200. 17 00:01:35,300 --> 00:01:39,340 If the status code is 200, we want to continue. 18 00:01:39,340 --> 00:01:42,680 If not, we want to print out an error message. 19 00:01:42,680 --> 00:01:46,522 The message will say, there was an error getting the profile for the username, 20 00:01:46,522 --> 00:01:48,080 with the status code at the end. 21 00:01:49,270 --> 00:01:53,957 Let's create a status code error, by creating a new error object. 22 00:01:53,957 --> 00:01:57,797 Inside the response callback, we'll check if the status code equals 200. 23 00:02:05,988 --> 00:02:11,053 If the status code isn't 200, we'll create a new error to handle this. 24 00:02:11,053 --> 00:02:17,329 We'll first create an error message where we'll interpolate the HTTP status code. 25 00:02:19,604 --> 00:02:24,538 And we'll need to include HTTP in our file, using require to do this. 26 00:02:27,693 --> 00:02:32,182 Actually copy this, remove the S in both places. 27 00:02:36,327 --> 00:02:39,925 Now on a new line, create a new error object. 28 00:02:54,160 --> 00:03:01,258 And now, we use printError, print out that statusCodeError. 29 00:03:06,806 --> 00:03:08,357 Save. 30 00:03:15,590 --> 00:03:17,754 There we have it. 31 00:03:17,754 --> 00:03:24,408 Our error message with the correct status code appended to the end. 32 00:03:24,408 --> 00:03:27,410 Well done, you've gotten acquainted with some of the errors and 33 00:03:27,410 --> 00:03:29,160 error types in Node. 34 00:03:29,160 --> 00:03:32,774 Take your learning further by getting familiar with the different possible 35 00:03:32,774 --> 00:03:34,249 errors in the documentation. 36 00:03:34,249 --> 00:03:37,783 When you get an error in your code, do your best to track where and 37 00:03:37,783 --> 00:03:39,780 why it is occurring. 38 00:03:39,780 --> 00:03:41,839 This will help you improve your debugging skills and 39 00:03:41,839 --> 00:03:43,820 getting a better understanding of the language.