1 00:00:00,270 --> 00:00:05,730 In this video we're going to take just a couple of minutes to talk about errors in node j s so we're 2 00:00:05,730 --> 00:00:10,040 gonna take our working program and we're gonna break it generating an error message. 3 00:00:10,140 --> 00:00:16,110 Then we'll look at its structure and figure out how we can use the information it provides us to easily 4 00:00:16,110 --> 00:00:19,120 figure out where in our program things are going wrong. 5 00:00:19,200 --> 00:00:25,350 And my hope is that this very short video can save you much more time in the long run by making it easier 6 00:00:25,350 --> 00:00:28,290 for you to find the issues in your application. 7 00:00:28,410 --> 00:00:33,870 So down below we're going to take our working app and break it by making a change that's going to cause 8 00:00:33,900 --> 00:00:36,120 an error to show up to do this. 9 00:00:36,120 --> 00:00:39,670 I'm going to change the code and save notes right here. 10 00:00:39,720 --> 00:00:45,200 We create a data Jason variable and then we reference it writing its content to a file. 11 00:00:45,210 --> 00:00:51,390 All I'm going to do is change the upper case as to a lower case one that's going to cause the script 12 00:00:51,540 --> 00:00:56,530 to crash as we're trying to reference a variable that's never been defined. 13 00:00:56,550 --> 00:01:00,170 Now let's go ahead and actually run the program to see the error message. 14 00:01:00,180 --> 00:01:01,460 We're gonna get down below. 15 00:01:01,470 --> 00:01:05,400 We have to use a command that would actually have save notes get called. 16 00:01:05,400 --> 00:01:07,560 That could be remove or add. 17 00:01:07,560 --> 00:01:09,000 I'll just use add. 18 00:01:09,000 --> 00:01:14,010 So node app dot J.S. add I'm going to add a new note with the following title. 19 00:01:14,040 --> 00:01:17,850 I'll use something like error message. 20 00:01:17,850 --> 00:01:19,780 Then I'll set up my body as well. 21 00:01:19,980 --> 00:01:21,420 And right here I can just use. 22 00:01:21,450 --> 00:01:22,410 Run it. 23 00:01:22,410 --> 00:01:23,310 Perfect. 24 00:01:23,310 --> 00:01:28,650 Now if I run this it is going to have the effect of running save notes when it tries to save our newly 25 00:01:28,650 --> 00:01:32,490 added note to the data store and the program is indeed going to crash. 26 00:01:32,490 --> 00:01:36,400 So let's go ahead and hit enter to generate the error message. 27 00:01:36,420 --> 00:01:43,590 Now if we scroll up we can break this down and the most important piece is right here after a new line 28 00:01:43,740 --> 00:01:46,680 maybe about four or five lines down. 29 00:01:46,740 --> 00:01:48,660 This is the actual error message. 30 00:01:48,660 --> 00:01:53,910 This is the explanation from v 8 as to why your program cut it run. 31 00:01:53,910 --> 00:02:00,060 In this case it is a reference error which is a whole category of errors and it has an explicit message 32 00:02:00,380 --> 00:02:01,100 data. 33 00:02:01,140 --> 00:02:06,350 Jason is not defined so this is letting us know exactly why things failed. 34 00:02:06,360 --> 00:02:08,600 But it's not really letting us know why. 35 00:02:08,640 --> 00:02:12,810 So it's still going to be up to us to figure out what's going wrong is data. 36 00:02:12,840 --> 00:02:18,990 Jason not defined because I've misspelled it or is it not defined because maybe Jason dot string ify 37 00:02:19,200 --> 00:02:22,260 didn't give us back what we thought it was going to give us back. 38 00:02:22,290 --> 00:02:24,660 So when we referenced it later it broke. 39 00:02:24,720 --> 00:02:30,720 So it'll still be up to us to really work through the problem that caused the error but the error can 40 00:02:30,720 --> 00:02:33,110 get us to the right part of the code. 41 00:02:33,300 --> 00:02:36,030 Now below the message what we just explored. 42 00:02:36,030 --> 00:02:41,700 We have a stack trace this contains a trace of all of the functions that are running to get to this 43 00:02:41,700 --> 00:02:48,330 point in the code and you'll notice the first line right after our message contains the call to save 44 00:02:48,330 --> 00:02:49,040 notes. 45 00:02:49,050 --> 00:02:55,140 So it's saying that we are at Save notes and it points to the correct file that is in Notes app. 46 00:02:55,140 --> 00:03:02,540 It is called Notes Doc J S and it even points to the line and the column so line 56 column 36. 47 00:03:02,550 --> 00:03:06,460 That is exactly right here where the problem occurred. 48 00:03:06,510 --> 00:03:12,930 So that first line after it your message contains the most useful information in the stack trace. 49 00:03:12,930 --> 00:03:18,990 As we go down the trace we typically tend to get less actionable information from here we can see that 50 00:03:18,990 --> 00:03:26,450 save notes was called by add note which was true that was called by handler which is true handler being 51 00:03:26,470 --> 00:03:30,750 the method we defined an app dot J S on line at twenty five. 52 00:03:30,870 --> 00:03:34,260 And if I go over there that's exactly what we're going to see. 53 00:03:34,320 --> 00:03:39,340 We have our handler method and on line twenty five we do indeed call add note. 54 00:03:39,480 --> 00:03:45,570 So it save notes to add note to handler and then after that we start to get into the internals in this 55 00:03:45,570 --> 00:03:53,070 case a lot of these are coming from yards we can see things related to run command pass args pass and 56 00:03:53,220 --> 00:03:58,950 others and the further we get down the list the closer we get to the node internals which in this case 57 00:03:58,980 --> 00:04:01,080 are these last three. 58 00:04:01,170 --> 00:04:06,420 So when we're looking at the stack trace we want to start from top to bottom as the top contains the 59 00:04:06,420 --> 00:04:10,550 most explicit pertinent information related to the error. 60 00:04:10,570 --> 00:04:12,990 That's where I'd like to stop for this video. 61 00:04:12,990 --> 00:04:18,930 You now know a bit about how to work with error messages and how to use the no debugger to debug your 62 00:04:18,930 --> 00:04:22,440 node applications using the Chrome developer tools. 63 00:04:22,440 --> 00:04:28,180 These are both techniques you can now use throughout the rest of the class to fix your issues faster. 64 00:04:28,200 --> 00:04:31,910 Now you're still going to make issues that is expected and completely normal. 65 00:04:32,070 --> 00:04:37,500 But it does get better over time as you get more comfortable with node you'll make less and less issues 66 00:04:37,530 --> 00:04:42,140 for yourself to solve and the ones you do create you'll be able to solve faster. 67 00:04:42,180 --> 00:04:46,600 But like everything it is a skill set that needs to be built up over time. 68 00:04:46,620 --> 00:04:50,690 We're gonna start to talk about asynchronous development in the next section. 69 00:04:50,700 --> 00:04:52,860 So let's go ahead and jump right into that.