1 00:00:00,000 --> 00:00:04,584 [MUSIC] 2 00:00:04,584 --> 00:00:08,392 As you may have already noticed, our apps are bound to have some errors, whether 3 00:00:08,392 --> 00:00:13,200 it's code, we've written or changes in the resources and dependencies we use. 4 00:00:13,200 --> 00:00:14,350 Thanks to the Error object. 5 00:00:14,350 --> 00:00:19,720 We can get feedback from errors occur and this helps us correct them. 6 00:00:19,720 --> 00:00:24,550 Think about a math test for example, if you handed in a math test, and the teacher 7 00:00:24,550 --> 00:00:29,680 only returns the test back to students who got a perfect score, that's not helpful. 8 00:00:30,980 --> 00:00:34,681 If you hand in the test, and the teacher marked which question you got wrong, 9 00:00:34,681 --> 00:00:36,040 that's somewhat helpful. 10 00:00:37,120 --> 00:00:40,915 If you handed in a test in the teacher indicating not only which question you got 11 00:00:40,915 --> 00:00:44,170 wrong but indicating what type of mistake you made. 12 00:00:44,170 --> 00:00:45,990 Maybe you forgot to carry the three. 13 00:00:45,990 --> 00:00:48,544 That's something you can learn from, done right. 14 00:00:48,544 --> 00:00:52,730 A JavaScript error handling can function like a great math teacher. 15 00:00:52,730 --> 00:00:55,595 They can tell you not only where your code stopped working, but 16 00:00:55,595 --> 00:00:58,360 also what type of issue caused the problem. 17 00:00:58,360 --> 00:01:03,431 There are different types of errors that can occur in node, and these include 18 00:01:03,431 --> 00:01:09,220 standard JavaScript errors, system errors, user specified errors and assertions. 19 00:01:09,220 --> 00:01:12,206 Were able to find out more about a given error, 20 00:01:12,206 --> 00:01:15,612 including its type by examining its properties. 21 00:01:15,612 --> 00:01:18,038 These include the error stack, 22 00:01:18,038 --> 00:01:22,220 which describes when the error occurred in the code. 23 00:01:22,220 --> 00:01:26,529 The error code, which tells us what kind of error, there's a great list in 24 00:01:26,529 --> 00:01:32,320 the documentation and the error message, which is a string describing the error. 25 00:01:32,320 --> 00:01:36,050 One method of error handling you might be familiar with is the try-catch block. 26 00:01:37,410 --> 00:01:41,254 try-catch block is an error handling technique used to execute a block 27 00:01:41,254 --> 00:01:41,830 of code and 28 00:01:41,830 --> 00:01:46,140 throw an error message if something goes wrong within the block to be executed. 29 00:01:47,210 --> 00:01:51,479 These are useful in JavaScript, but with many of Node's asynchronous methods, 30 00:01:51,479 --> 00:01:56,067 the try-catch block is executed completely before Node.js methods have a chance to 31 00:01:56,067 --> 00:01:57,440 finish. 32 00:01:57,440 --> 00:01:59,854 Because Node.js is asynchronous in nature, 33 00:01:59,854 --> 00:02:03,390 we will handle most errors in Node using asynchronous methods. 34 00:02:05,450 --> 00:02:09,366 Let's intentionally create an error in our project, 35 00:02:09,366 --> 00:02:14,443 we remove the period from URL and then save, and try to run our code. 36 00:02:14,443 --> 00:02:20,409 We get a long error message that may be difficult for app users to interpret. 37 00:02:20,409 --> 00:02:24,110 The error messages includes the code ENOTFOUND. 38 00:02:25,710 --> 00:02:28,470 We can look this up in the node error documentation and 39 00:02:28,470 --> 00:02:31,950 find out if this is a result of the address not being found. 40 00:02:31,950 --> 00:02:34,476 Which makes sense for an address that doesn't exist. 41 00:02:36,721 --> 00:02:41,068 We can use the tools that node gives us to handle this error in a more user friendly, 42 00:02:41,068 --> 00:02:41,950 organized way. 43 00:02:43,060 --> 00:02:47,260 An incorrect but valid URL in the asynchronous block of code won't be caught 44 00:02:47,260 --> 00:02:50,190 by something like a try catch block. 45 00:02:50,190 --> 00:02:52,946 It's not until runtime or the code is executed and 46 00:02:52,946 --> 00:02:55,970 the async call starts, that this error will be recognized. 47 00:02:57,970 --> 00:03:02,367 Just like we watch for data in events earlier, we can watch for errors as well. 48 00:03:06,651 --> 00:03:11,380 On the request object we will watch for events with on. 49 00:03:14,330 --> 00:03:15,750 Watch out for the error event. 50 00:03:18,690 --> 00:03:21,421 Give this a callback function that takes the error. 51 00:03:24,940 --> 00:03:30,195 And will use the console.error method to print out the error. 52 00:03:34,560 --> 00:03:40,411 I will save and will try it again and this has removed some of the extra text, 53 00:03:40,411 --> 00:03:46,380 but we can go even further and narrow down specific properties 54 00:03:46,380 --> 00:03:53,481 of the Error object. Let's go with the error code for example. 55 00:04:01,008 --> 00:04:01,630 There we go. 56 00:04:02,670 --> 00:04:04,830 Now many users may not know what these codes mean. 57 00:04:06,370 --> 00:04:09,183 We can write a custom error message in this handler as well. 58 00:04:24,257 --> 00:04:25,584 There is our custom message. 59 00:04:25,584 --> 00:04:27,340 Now, a bad argument, 60 00:04:27,340 --> 00:04:32,343 like removing the HTTPS protocol will cause an error right away. 61 00:04:32,343 --> 00:04:34,904 When we remove the protocol from our URL, 62 00:04:40,153 --> 00:04:46,402 Add the period back, save, try running again. 63 00:04:49,258 --> 00:04:52,240 We get an invalid URL error. 64 00:04:52,240 --> 00:04:55,640 This is caused by a missing protocol or invalid URL. 65 00:04:55,640 --> 00:04:58,958 The asynchronous call cannot be made without the correct protocol so 66 00:04:58,958 --> 00:05:01,330 this error is synchronous. 67 00:05:01,330 --> 00:05:04,650 In this instance, the program will be unable to continue running. 68 00:05:04,650 --> 00:05:09,064 Because of this, it is a rare case where we can use a try-catch block in Node. 69 00:05:10,798 --> 00:05:12,816 We'll wrap the request in a try block. 70 00:05:25,421 --> 00:05:30,470 We'll catch the error, and 71 00:05:30,470 --> 00:05:35,141 use console.error again to log out the error message, 72 00:05:43,970 --> 00:05:47,530 And there we have it only the error message appears. 73 00:05:47,530 --> 00:05:52,320 We can again customize this even further to make the message our own. 74 00:05:52,320 --> 00:05:56,245 Great work, in the next video we'll look more at the try-catch block for 75 00:05:56,245 --> 00:05:57,080 parsing errors.