1 00:00:00,590 --> 00:00:04,420 In the last couple of videos we've spoken a little bit about area handling and now we're at the point 2 00:00:04,420 --> 00:00:10,420 where I think that we've got two distinct driving forces in doing all this area handling stuff So issue 3 00:00:10,420 --> 00:00:11,950 number one with air handling. 4 00:00:12,010 --> 00:00:16,330 We need to make sure that we always have a very consistently structured response from all of our different 5 00:00:16,330 --> 00:00:21,820 services we put together and no matter what goes wrong in any of those services whether it is some invalid 6 00:00:21,850 --> 00:00:27,640 data into a request handler or an error in database access or some other error we need to make sure 7 00:00:27,640 --> 00:00:32,980 that we somehow capture all those different things successfully and structure the air response in some 8 00:00:32,980 --> 00:00:37,520 very consistent fashion that is driving force number one and number two. 9 00:00:38,320 --> 00:00:42,300 Let's talk about how we are going to solve each of these issues. 10 00:00:42,370 --> 00:00:42,630 All right. 11 00:00:42,640 --> 00:00:49,450 So a little solution here so to make sure that we always capture all errors and process them and structure 12 00:00:49,450 --> 00:00:52,330 them in some identical consistent fashion. 13 00:00:52,420 --> 00:00:58,220 We're going to write an error handling middleware we're then going to wire that up to express and anytime 14 00:00:58,300 --> 00:01:02,560 that something goes wrong instead of application Express is going to capture some kind of error and 15 00:01:02,560 --> 00:01:04,430 send it to our area handling middleware. 16 00:01:04,510 --> 00:01:09,140 So we're going to do some processing inside of this area handling middleware. 17 00:01:09,260 --> 00:01:14,300 The other big thing we need to make sure that no matter what goes wrong no matter where some error occurs 18 00:01:14,710 --> 00:01:19,940 we need to somehow capture it and send it off to express so that it can be forwarded on to our area 19 00:01:19,940 --> 00:01:24,980 handling middleware so we're going to make sure that we capture all possible errors inside of all of 20 00:01:24,980 --> 00:01:29,480 our different requests handlers and anytime something goes wrong we're going to pass the error off to 21 00:01:29,480 --> 00:01:34,880 express and effectively that really just means using the next function inside of our request handlers 22 00:01:35,480 --> 00:01:36,950 that might not make a lot of sense right now. 23 00:01:37,120 --> 00:01:42,450 So let me give you a quick reminder on how EXPRESS Does your handling in general I'm taking a look at 24 00:01:42,450 --> 00:01:45,410 the Express area handling documentation right here. 25 00:01:45,560 --> 00:01:47,930 So there are several sections inside of here. 26 00:01:47,970 --> 00:01:52,360 Overall it's a two step process for dealing with errors inside of Express. 27 00:01:52,380 --> 00:01:57,270 First off if anything ever goes wrong inside of a synchronous root handler and that's what you see right 28 00:01:57,270 --> 00:01:57,790 here. 29 00:01:57,870 --> 00:02:01,180 It is a synchronous right handler because there is no async code inside of it. 30 00:02:01,350 --> 00:02:08,250 No callbacks no promises no async await if anything ever throws an error that Express is going to automatically 31 00:02:08,250 --> 00:02:14,660 capture that error and forward it on to any error handling middleware that you and I have authored we 32 00:02:14,660 --> 00:02:19,230 can then do some processing on the air and automatically send a response back. 33 00:02:19,260 --> 00:02:24,150 The second case in which we'll get an error is when we do have an async request handler. 34 00:02:24,150 --> 00:02:29,990 So this is an example of an async request handler because it has an async callback inside of here. 35 00:02:30,000 --> 00:02:34,440 This code is going to try to read a file off the hard drive and then at some point in time the future 36 00:02:34,470 --> 00:02:36,830 that callback function gets invoked. 37 00:02:36,840 --> 00:02:43,200 This is also appropriate or relevant if you're making use of promises or the async await syntax so if 38 00:02:43,200 --> 00:02:48,740 you have an async root handler and something goes wrong you and I need to manually capture that error 39 00:02:49,190 --> 00:02:54,930 either by receiving it as an argument into a callback function or setting up a try catch block. 40 00:02:55,250 --> 00:03:00,380 We're then going to take that error and pass it off to the next function that is provided automatically 41 00:03:00,380 --> 00:03:02,510 into our root handler. 42 00:03:02,520 --> 00:03:07,120 So again we've got scenario number one but they synchronous root handler Express is going to take care 43 00:03:07,120 --> 00:03:12,510 of that automatically for us And scenario number two where we have an async root handler and that's 44 00:03:12,510 --> 00:03:20,430 going to be any root handler that makes use of a callback function promise or async await syntax so 45 00:03:20,700 --> 00:03:25,770 that is how we're going to somehow capture in air and pass it off to the next function to actually build 46 00:03:25,830 --> 00:03:27,060 a error handling middleware. 47 00:03:27,060 --> 00:03:28,990 Let me give you a quick reminder on that as well. 48 00:03:29,160 --> 00:03:31,610 On the same documentation page I'm going to scroll down a little bit. 49 00:03:34,380 --> 00:03:37,950 And there are some directions on here for writing an error handler. 50 00:03:37,950 --> 00:03:42,810 So we defined a middleware function and all we have to do is make sure that the middleware function 51 00:03:43,020 --> 00:03:48,660 is going to receive four different arguments whenever we pass a middleware into Express. 52 00:03:48,660 --> 00:03:53,940 It's going to automatically pass that function and count the number of arguments so express is essentially 53 00:03:53,940 --> 00:03:59,220 aware of the number of arguments that are going into a middleware if we have a middleware function with 54 00:03:59,220 --> 00:04:04,110 four arguments Express is going to assume that this is a function meant to handle errors that occur 55 00:04:04,290 --> 00:04:06,820 inside of one of our different root handlers. 56 00:04:06,900 --> 00:04:10,680 So you and I are going to write out a function that looks very similar to this right here. 57 00:04:10,770 --> 00:04:14,970 We're gonna make sure that any time it gets called we somehow inspect that error figure out what goes 58 00:04:14,970 --> 00:04:20,590 wrong and then automatically send a response back now in all the stuff you're going to start to see 59 00:04:20,590 --> 00:04:23,450 that there are a couple of interesting corner cases. 60 00:04:23,450 --> 00:04:28,370 So right now I'm presenting you a very simple and straightforward way of dealing with this air stuff. 61 00:04:28,440 --> 00:04:32,950 Well we are going to publish this stuff up beyond what you see in this documentation to a pretty good 62 00:04:32,950 --> 00:04:33,590 degree. 63 00:04:33,650 --> 00:04:38,710 We're going to eventually have a pretty sweet little airy handling process now that we understand the 64 00:04:38,770 --> 00:04:40,780 two general things that we're going to do here. 65 00:04:40,780 --> 00:04:43,450 Number one we're going to make sure we capture all possible layers. 66 00:04:43,450 --> 00:04:45,900 Number two we're also going to write an error handling middleware. 67 00:04:45,910 --> 00:04:49,690 Let's take a pause right here and start to implement both of these things in the next video.