1 00:00:00,450 --> 00:00:02,510 Remember back at the beginning of the course, 2 00:00:02,510 --> 00:00:04,360 when we made our first request of the app. 3 00:00:05,610 --> 00:00:10,000 The client received a 404 error back from our route. 4 00:00:10,000 --> 00:00:13,965 This meant, Express didn't have the page that we requested. 5 00:00:13,965 --> 00:00:18,150 More specifically, it meant that there was no get route for 6 00:00:18,150 --> 00:00:20,710 our app matching the URL we requested. 7 00:00:21,710 --> 00:00:25,750 Applications always need a way of handling errors like this. 8 00:00:26,750 --> 00:00:31,620 Other errors might occur when the app can't reach a database, or 9 00:00:31,620 --> 00:00:33,620 when a user mistype their password. 10 00:00:34,930 --> 00:00:36,980 Errors are an important tool for 11 00:00:36,980 --> 00:00:39,950 your user as they learn how to interact with your app. 12 00:00:41,160 --> 00:00:45,330 They offer information about the limits of how your app can be used. 13 00:00:46,480 --> 00:00:50,710 Errors also guide developers in fixing bugs in an application. 14 00:00:50,710 --> 00:00:55,700 In Express, you can use the next function to signal an error in your app. 15 00:00:55,700 --> 00:00:59,840 By passing an object as a parameter to next, 16 00:00:59,840 --> 00:01:03,610 Express knows there is an error to handle. 17 00:01:03,610 --> 00:01:08,133 Express will then immediately terminate and handle the error. 18 00:01:08,133 --> 00:01:10,565 Let's create our own error. 19 00:01:15,430 --> 00:01:24,575 We'll use the new error constructor, And pass in a string. 20 00:01:24,575 --> 00:01:27,200 The string will serve as an error message. 21 00:01:30,410 --> 00:01:34,189 Normally, no, wouldn't be a good error string in a real app, 22 00:01:34,189 --> 00:01:37,788 because it doesn't provide any clues about what went wrong. 23 00:01:37,788 --> 00:01:42,502 But as you'll see soon there is a lot of text when an error is thrown, and 24 00:01:42,502 --> 00:01:47,390 hopefully this text will be easy to pick out when we view it on the browser. 25 00:01:48,570 --> 00:01:51,980 This is JavaScript's native error constructor. 26 00:01:51,980 --> 00:01:54,430 If you haven't seen this syntax before or 27 00:01:54,430 --> 00:01:57,540 haven't seen it too often, don't worry about it for now. 28 00:01:58,870 --> 00:02:04,000 It's enough to know that we're creating a custom error object and storing it in ERR. 29 00:02:05,440 --> 00:02:09,190 Look in the teacher's notes if you'd like to read more about JavaScript's 30 00:02:09,190 --> 00:02:10,070 error constructor. 31 00:02:11,600 --> 00:02:17,090 Let's pass in the error object as an argument to the next function call. 32 00:02:17,090 --> 00:02:22,980 If I save it and refresh the browser, there is a lot of text here. 33 00:02:22,980 --> 00:02:30,660 The error is thrown and at the top, you can see the error message was outputted. 34 00:02:30,660 --> 00:02:37,143 The next line tells us that the error was thrown from line 14. 35 00:02:37,143 --> 00:02:40,937 The second number is the column, or 36 00:02:40,937 --> 00:02:46,640 how many spaces from the first position on line 14. 37 00:02:46,640 --> 00:02:50,550 Pinpointing where an error is in an app can really speed up the process of 38 00:02:50,550 --> 00:02:52,180 fixing bugs. 39 00:02:52,180 --> 00:02:56,369 All of the lines after this indicate deeper layers of the Express framework, 40 00:02:56,369 --> 00:02:59,540 and you probably won't need to go into this. 41 00:02:59,540 --> 00:03:02,560 Sometimes though, if you really stumped, 42 00:03:02,560 --> 00:03:07,450 pasting this into a Google search can get you closer to your answer. 43 00:03:07,450 --> 00:03:09,158 Let's check the console. 44 00:03:11,030 --> 00:03:15,720 And you can see Hello is logged out just above where the error was thrown. 45 00:03:17,730 --> 00:03:24,090 World never gets logged, because the program was interrupted by the error. 46 00:03:25,690 --> 00:03:27,010 Let's switch back to the browser. 47 00:03:28,570 --> 00:03:32,890 This output is coming from Express's built in error handler. 48 00:03:32,890 --> 00:03:37,460 It has some good information in here, but the formatting has some room for 49 00:03:37,460 --> 00:03:38,000 improvement. 50 00:03:39,690 --> 00:03:44,470 More readable error messages can make development an easier process. 51 00:03:44,470 --> 00:03:48,930 Express offers a way to write our own custom error handlers, 52 00:03:48,930 --> 00:03:53,395 where we can format the errors to be much more readable. 53 00:03:53,395 --> 00:03:55,130 I'll show yo how to do that next.