1 00:00:00,380 --> 00:00:03,190 Let me show you another error that can occur. 2 00:00:03,190 --> 00:00:09,550 If you visit my profile endpoint at teamtreehouse.com/chalkers.json 3 00:00:09,550 --> 00:00:14,630 we see a beautiful JSON file application, and we can work with this. 4 00:00:14,630 --> 00:00:20,205 Well, what about if we go to another profile that doesn't exist, like 5 00:00:20,205 --> 00:00:29,260 https://teamtreehouse.com/chalkers-not and hit Enter. 6 00:00:31,100 --> 00:00:32,850 There's the text not found. 7 00:00:32,850 --> 00:00:37,080 The text not found is not a valid JSON object. 8 00:00:38,440 --> 00:00:41,434 Let's run the application with chalkers not as the username. 9 00:00:47,070 --> 00:00:50,445 And this is the error that's displayed. 10 00:00:50,445 --> 00:00:54,170 Our application is attempting to parse the string not found. 11 00:00:55,990 --> 00:00:59,750 Not found isn't a valid JavaScript object. 12 00:00:59,750 --> 00:01:06,920 That's why it say's unexpected token, meaning character n. 13 00:01:08,710 --> 00:01:12,020 n is the first character it's trying to parse. 14 00:01:12,020 --> 00:01:17,390 JSON files start with either an opening square bracket or curly braces. 15 00:01:19,150 --> 00:01:22,495 Let's wrap the lines of code dealing with the parsing and 16 00:01:22,495 --> 00:01:24,959 printing the message out with a try block. 17 00:01:37,684 --> 00:01:41,862 Then we can catch the error and print it out. 18 00:01:51,904 --> 00:01:52,787 Save the file. 19 00:01:55,730 --> 00:01:59,230 And then rerun the application with an invalid username and 20 00:01:59,230 --> 00:02:01,400 we don't get a stack trace anymore. 21 00:02:02,960 --> 00:02:07,580 Once again, you can go ahead and include a more user-friendly message if you want. 22 00:02:08,820 --> 00:02:11,750 Before we move on to the next error we handle, 23 00:02:11,750 --> 00:02:15,580 since we're handling multiple errors, let's create a function that's 24 00:02:15,580 --> 00:02:20,300 flexible enough to print out error messages from an error object. 25 00:02:20,300 --> 00:02:23,380 Or any other object with a message property. 26 00:02:23,380 --> 00:02:27,685 This means if we want to choose to implement how we handle errors differently 27 00:02:27,685 --> 00:02:30,456 in the future, we only have to do it in one place. 28 00:02:39,146 --> 00:02:42,652 Let's create a function PrintError. 29 00:02:45,068 --> 00:02:46,578 That takes in one argument error. 30 00:02:57,339 --> 00:03:00,075 And log out the error message. 31 00:03:00,075 --> 00:03:04,639 Let's update where all the console.errors are with printError. 32 00:03:27,211 --> 00:03:27,711 Awesome.