1 00:00:00,970 --> 00:00:06,190 We've now got a working demonstration of how to create custom airs how to throw them from our different 2 00:00:06,190 --> 00:00:12,280 root handlers how to capture them inside of our air handler and send back a very common response for 3 00:00:12,280 --> 00:00:15,390 every different kind of air but we're not quite done yet. 4 00:00:15,430 --> 00:00:20,500 I want to point out something not so great about the current implementation of our airy handling middleware. 5 00:00:20,500 --> 00:00:25,870 Right now we are throwing our request validation error and the database felt connection error into that 6 00:00:25,930 --> 00:00:32,950 area handling middleware and we've encoded inside there some very intricate knowledge of exactly how 7 00:00:32,950 --> 00:00:37,000 to extract information from every kind of error that exists inside our application. 8 00:00:37,000 --> 00:00:40,300 Now present that's just two errors so it's not that big a deal. 9 00:00:40,330 --> 00:00:44,800 Well let's imagine some scenario down the line where maybe we have a lot of different things that can 10 00:00:44,800 --> 00:00:50,890 go wrong inside of our application and we create a custom error for everything that can go wrong. 11 00:00:50,890 --> 00:00:53,680 And so we end up with all these different custom errors like this. 12 00:00:54,580 --> 00:00:58,690 If we travel down the same road that we're going down right now our error handily Middleware is going 13 00:00:58,690 --> 00:01:01,300 to be grow to be absolutely gigantic. 14 00:01:01,300 --> 00:01:07,270 We will have to encode some logic inside there to understand every possible error and understand how 15 00:01:07,270 --> 00:01:13,930 to extract information from them to send down to a user making sure that they all follow the same kind 16 00:01:13,930 --> 00:01:15,410 of a common response structure. 17 00:01:16,470 --> 00:01:20,720 So right now if we continue down the same path we're going to end up with our area handling middleware 18 00:01:20,750 --> 00:01:23,990 growing to be extremely extremely large. 19 00:01:24,050 --> 00:01:25,840 How can we fix this. 20 00:01:25,850 --> 00:01:30,570 Well we're going to kind of inverse or take the opposite of this relationship. 21 00:01:30,670 --> 00:01:35,600 We're going to go back and revisit our request validation error and the database connection error. 22 00:01:35,890 --> 00:01:40,620 We're going to add in a method to each of these called serialize error. 23 00:01:40,670 --> 00:01:46,610 The goal of this method is to take all the information about the air so whatever fields failed validation 24 00:01:47,000 --> 00:01:52,520 or some reason that we failed to connect to the database and we're going to return some array of objects 25 00:01:52,730 --> 00:01:56,020 that follow this kind of common error structure right here. 26 00:01:56,060 --> 00:02:00,290 So an array of objects that have that message that the string and a possibly a field that is a string 27 00:02:00,320 --> 00:02:06,510 as well we're also going to make sure that all these different errors list a status code to send down 28 00:02:06,510 --> 00:02:08,930 as well once we do this. 29 00:02:08,950 --> 00:02:14,890 Our air handler Middleware is no longer going to have to understand how to precisely pass and somehow 30 00:02:15,100 --> 00:02:17,130 interpret a request validation error. 31 00:02:17,380 --> 00:02:23,140 Instead the error handling middleware can just verify to make sure that the incoming air is of type 32 00:02:23,140 --> 00:02:28,360 request validation error or database connection error wants to make sure that's an error that it understands 33 00:02:28,810 --> 00:02:35,210 it can then call that errors serialize their property and get back some very customized very well-formed 34 00:02:35,230 --> 00:02:39,220 structure to describe what just went wrong and send that back to the user. 35 00:02:39,310 --> 00:02:44,350 It can also refer to the errors status code to understand what status code to use inside the response 36 00:02:44,350 --> 00:02:45,010 as well. 37 00:02:46,820 --> 00:02:52,490 Once we start to follow this approach we can create as many air custom classes as we want. 38 00:02:52,490 --> 00:02:57,140 We can make a billion of these different things all very custom in nature each one can have its own 39 00:02:57,140 --> 00:03:02,820 special implementation and the error handling middle way Middleware is not going to grow in much complexity 40 00:03:03,240 --> 00:03:08,130 because no matter what the areas we're always going to refer to the same kind of serialize their function 41 00:03:08,160 --> 00:03:10,840 and the same status code property. 42 00:03:10,860 --> 00:03:15,690 OK so then mind let's go back over to our Ed we're going to start to refactor database connection there 43 00:03:16,470 --> 00:03:19,590 and our other one that's behind the scenes here. 44 00:03:19,680 --> 00:03:24,990 Request validation one we're going to make sure that they both have a serialized error method and a 45 00:03:24,990 --> 00:03:26,880 status code property as well. 46 00:03:26,910 --> 00:03:31,260 We're going to make sure that the air handler uses those methods and those properties as opposed to 47 00:03:31,260 --> 00:03:36,120 trying to generate this formatted error stuff on its own. 48 00:03:36,120 --> 00:03:39,200 All right so let's first start off with how about database connection error. 49 00:03:39,360 --> 00:03:40,080 Here it is right here 50 00:03:43,310 --> 00:03:45,210 the inside of database connection error. 51 00:03:45,450 --> 00:03:52,940 We're going to add in that serialize errors method and inside of here we're going to make sure that 52 00:03:52,950 --> 00:03:56,820 we return this common air structure specifically. 53 00:03:56,880 --> 00:04:02,280 I really just want to return the array of objects that have a message and the field we will still allow 54 00:04:02,520 --> 00:04:09,250 our error Hendley middleware to create this actual object and assign the errors property to it so inside 55 00:04:09,280 --> 00:04:15,430 of database connection error I've got serialize errors we're going to return an array of objects in 56 00:04:15,430 --> 00:04:20,140 this case the object they're gonna create is very simple it's always going to be one single error object 57 00:04:20,440 --> 00:04:24,610 that's going to list this reason as the message so we can say. 58 00:04:24,610 --> 00:04:33,010 Message is this dot reason and then in addition I'm also going to assign a status code to this. 59 00:04:33,080 --> 00:04:38,630 So we're going to say anytime a database connection error occurs let's give the response a status code 60 00:04:38,690 --> 00:04:40,220 of five hundred. 61 00:04:40,220 --> 00:04:45,200 Now in this case again I want to remind you that database connection error is very simplistic in nature. 62 00:04:45,200 --> 00:04:48,400 Yes we don't really need to define the separate property of reason right here. 63 00:04:48,410 --> 00:04:51,420 We could just take that string and throw it in as the message. 64 00:04:51,470 --> 00:04:56,600 The only reason that we are reading this entire database connection air class is that you have a second 65 00:04:56,600 --> 00:04:59,700 example to look at beyond just request validation error. 66 00:04:59,750 --> 00:05:04,030 The other air class we created OK so that's it for this one. 67 00:05:04,050 --> 00:05:10,570 I'm going to say this file will then go over to request validation error also inside of our ears directory 68 00:05:11,210 --> 00:05:14,290 I'm going to give this one a status code as well. 69 00:05:14,310 --> 00:05:18,910 Four hundred in this case and then I will give it that serialize heirs property 70 00:05:22,270 --> 00:05:26,590 and we can really just take the code that we are already wrote out inside of our area handling middleware. 71 00:05:26,590 --> 00:05:28,670 It's going to go backwards of the error handler. 72 00:05:28,690 --> 00:05:32,990 You'll recall that we mapped over the errors and where each error. 73 00:05:33,010 --> 00:05:35,920 We returned an object that a message and a field. 74 00:05:36,100 --> 00:05:39,400 So we essentially want to do that same thing inside of this serialize errors. 75 00:05:39,610 --> 00:05:47,220 Function right here I'm going to return the result of this dot errors dot map. 76 00:05:47,390 --> 00:05:55,940 I'm going to take each error which I will abbreviate as you are and then return message with E R dot 77 00:05:56,180 --> 00:06:04,740 message and a field with E R dot Ram OK. 78 00:06:04,850 --> 00:06:08,760 So now are two errors both follow the same kind of structure. 79 00:06:08,780 --> 00:06:12,470 They both had the serialized errors method and they both have status code. 80 00:06:12,470 --> 00:06:16,060 So now back inside of our air handling middleware there it is right here. 81 00:06:16,220 --> 00:06:21,800 We no longer have to encode all this fancy logic into this middleware so we can delete all that fancy 82 00:06:21,800 --> 00:06:27,040 logic we can also do the same thing down here for the database connection error as well. 83 00:06:27,260 --> 00:06:31,580 So we no longer have to encode this entire special array right here which is kind of assuming that we 84 00:06:31,580 --> 00:06:35,390 have a lot of special knowledge about how database connection error works. 85 00:06:35,620 --> 00:06:40,840 It's going to delete that to also formatted errors goes away up here because that variable doesn't exist 86 00:06:40,840 --> 00:06:42,070 anymore. 87 00:06:42,070 --> 00:06:47,320 Now in both cases we want to take a look at that incoming air and we're going to call these serialized 88 00:06:47,380 --> 00:06:49,070 errors function on both them. 89 00:06:49,150 --> 00:06:53,460 That's going to give us back that array of objects that we want so so badly apparently. 90 00:06:53,500 --> 00:06:56,760 I'm definitely making it seem like it's a very desirable thing to have. 91 00:06:56,770 --> 00:06:57,120 OK. 92 00:06:57,130 --> 00:07:05,440 So in both cases we'll do ya dot serialize errors and then same thing down here as well 93 00:07:08,280 --> 00:07:15,490 and that should be at you'll now notice that we have an identical statement here with both these f statements. 94 00:07:15,490 --> 00:07:19,600 Once we have verified that we have either a request validation error or a database connection error 95 00:07:20,000 --> 00:07:25,360 were then very sure that we pretty much always want to do the same thing of getting serialize errors. 96 00:07:25,420 --> 00:07:29,110 One other thing that we want to take care of is the status codes on both as well. 97 00:07:29,140 --> 00:07:33,710 So now our middleware doesn't need to know about the precise status code to use for any kind of error. 98 00:07:33,730 --> 00:07:38,680 Instead it just knows to take a look at the errors status code property so and replace the 400 right 99 00:07:38,680 --> 00:07:50,070 there with IRR status code and the 500 right here with ya dot status code as well all these definitely 100 00:07:50,070 --> 00:07:54,480 look very identical in nature and as you can guess we're probably going to combine these two statements 101 00:07:54,570 --> 00:07:58,860 at some point but right now let's just say this and do another quick test to make sure that everything 102 00:07:58,860 --> 00:08:05,540 is still working as expected so back over at postmen I'm going to first try out the database connection 103 00:08:05,540 --> 00:08:12,730 error stuff I can send that off you still get the correct error and if I put in an invalid email you 104 00:08:12,820 --> 00:08:15,510 still get the rector as well. 105 00:08:15,520 --> 00:08:15,820 All right. 106 00:08:15,910 --> 00:08:20,530 So right away you can start to see that we are going to extract a lot of logic out of this area handling 107 00:08:20,550 --> 00:08:25,540 middleware and we're going to delegate figuring out how to produce the correct structure of errors and 108 00:08:25,540 --> 00:08:29,810 the quick status code in each error instead. 109 00:08:29,910 --> 00:08:34,350 This is looking pretty good but we still have one or two very small improvements so quick pause right 110 00:08:34,350 --> 00:08:35,900 here and I'll see you in just a minute.