1 00:00:01,830 --> 00:00:03,690 Our error handling Middleware is looking pretty good. 2 00:00:03,690 --> 00:00:07,530 But as I've said several times just one or two quick more things. 3 00:00:07,620 --> 00:00:10,240 So the next thing I want to take a look at is inside this middleware. 4 00:00:10,410 --> 00:00:13,670 I just want to really study these two if statements for a moment. 5 00:00:13,710 --> 00:00:18,690 At present we are checking to see if the error is of type request validation error or database connection 6 00:00:18,690 --> 00:00:20,050 error if it is. 7 00:00:20,050 --> 00:00:25,770 We then call the errors serialize errors method stick it into this bigger object and then send the result 8 00:00:25,770 --> 00:00:26,480 back to the user. 9 00:00:27,420 --> 00:00:33,160 There's a very big potential for a mistake inside of here right now let's imagine for a second that 10 00:00:33,160 --> 00:00:36,130 someone is building out a new custom error class. 11 00:00:36,190 --> 00:00:40,460 So let's imagine that we are building out to request validation error for the very first time. 12 00:00:40,750 --> 00:00:44,560 And I sit down to implement this serialize errors function. 13 00:00:44,560 --> 00:00:48,700 So let's imagine we're writing this out for the very first time and maybe for some accidental reason 14 00:00:48,970 --> 00:00:51,490 we make a little typo in message. 15 00:00:51,490 --> 00:00:54,250 Maybe I type in mage instead of message. 16 00:00:54,250 --> 00:01:00,220 At present there is absolutely nothing inside of our typescript code to verify and make sure that serialize 17 00:01:00,250 --> 00:01:02,440 errors is put together correctly. 18 00:01:02,530 --> 00:01:08,020 It would be really nice if we had some kind of check inside of our code something to make sure that 19 00:01:08,020 --> 00:01:11,070 serialize theirs is always going to return. 20 00:01:11,140 --> 00:01:16,330 This kind of structure right here that nice array of objects that we are doing all this work to make 21 00:01:16,330 --> 00:01:22,650 sure we get the right now and these errors flow into our error handling middleware. 22 00:01:22,750 --> 00:01:26,530 We're just checking to see if we have an instance of database connection error or request validation 23 00:01:26,530 --> 00:01:27,260 error. 24 00:01:27,430 --> 00:01:32,740 If we do so if we say yes that's what these this error is our air handler Middleware is pretty much 25 00:01:32,740 --> 00:01:37,930 just saying Gee I sure hope you implemented that serialize errors function correctly. 26 00:01:37,930 --> 00:01:43,810 It's really just a hope it would be kind of nice to actually have something in the typescript world 27 00:01:44,120 --> 00:01:49,180 to just double check and make sure that these custom errors have the correct implementation of serialized 28 00:01:49,180 --> 00:01:54,340 errors that would be something really nice to add in because we'd make sure that as we start to add 29 00:01:54,670 --> 00:01:59,620 some additional custom error classes in the future we want to make sure that we've always creating a 30 00:01:59,620 --> 00:02:04,570 custom area that has a status code that's a number and has a serialized errors function that's going 31 00:02:04,570 --> 00:02:10,570 to return the correct structure of data as again we're going through all this extra effort just to make 32 00:02:10,570 --> 00:02:14,500 sure that all of our different servers that we're gonna create over time are always going to return 33 00:02:14,500 --> 00:02:16,450 the exact same structure of error. 34 00:02:16,510 --> 00:02:20,230 So again it'd just be really great to make sure that we had something inside the typescript world to 35 00:02:20,230 --> 00:02:22,260 double check that. 36 00:02:22,480 --> 00:02:23,830 So how can we do this. 37 00:02:23,860 --> 00:02:26,200 Well there are two possible approaches. 38 00:02:26,200 --> 00:02:27,430 Here's option number one. 39 00:02:27,820 --> 00:02:28,800 Option number one. 40 00:02:28,900 --> 00:02:33,860 We could create a new file inside of our ears directory and we would call this file something like custom 41 00:02:33,860 --> 00:02:40,220 air inside there we would define an interface called Custom air so inside this custom air interface 42 00:02:40,280 --> 00:02:43,350 we would define what it means to be a customer. 43 00:02:43,490 --> 00:02:47,630 We could say that in order to be a customer you must have a status code that is a number and you must 44 00:02:47,630 --> 00:02:52,760 implement a serialized erase function that returns an array of objects that has a message property and 45 00:02:52,760 --> 00:02:58,770 possibly a field property we could then import that into our request validation and database connection 46 00:02:58,770 --> 00:03:05,830 files and use that implements keyword to make sure that those classes correctly implement that interface. 47 00:03:05,840 --> 00:03:08,810 Now this is a very very viable option. 48 00:03:08,810 --> 00:03:11,740 Absolutely not anything inherently wrong with this. 49 00:03:11,850 --> 00:03:17,180 And because that I want to is very quickly like really quickly write out the code to do something like 50 00:03:17,180 --> 00:03:20,580 this just to make sure it's really crystal clear. 51 00:03:20,680 --> 00:03:22,510 Now we are gonna delete this code immediately. 52 00:03:22,510 --> 00:03:24,330 Again I just wanna give you a very quick example. 53 00:03:24,370 --> 00:03:27,250 So you do not have to write this out if you don't want to. 54 00:03:27,250 --> 00:03:32,680 So as an example normally I would define this interface inside of a separate file but just to save some 55 00:03:32,680 --> 00:03:37,960 time all right the interface directly inside of request validation error but it could do something inside 56 00:03:37,960 --> 00:03:40,250 of here like customer. 57 00:03:40,270 --> 00:03:47,610 We could say that in order to be a customer you have to have a status code that is a number and you 58 00:03:47,610 --> 00:03:55,040 must have a serialized erase function that's going to return an array of objects and each of those objects 59 00:03:55,610 --> 00:04:04,100 should have a message that is a string and optionally a field the question mark right there says that 60 00:04:04,100 --> 00:04:07,820 field is not necessary but you can optionally provide it. 61 00:04:07,880 --> 00:04:12,680 So now that this interfaces put together we can make sure that our custom class satisfies this interface 62 00:04:12,920 --> 00:04:18,040 by writing onto the very end of the class declaration implements custom air. 63 00:04:18,050 --> 00:04:23,890 Like so now this is double check going to double check and make sure that I've got a status quo that 64 00:04:23,890 --> 00:04:28,660 has a number and a function called serialize errors that returns an array of objects that look like 65 00:04:28,660 --> 00:04:33,640 that as soon as I add on implements custom air I can scroll down and see that I've now gotten error 66 00:04:33,640 --> 00:04:34,900 around serialize errors. 67 00:04:35,020 --> 00:04:40,510 And that's because I have a function it is called serialize errors but is returning the incorrect structure 68 00:04:40,510 --> 00:04:42,650 of data rather than saying message. 69 00:04:42,670 --> 00:04:43,870 It says mage. 70 00:04:43,870 --> 00:04:46,580 And so this would help me catch errors in my code. 71 00:04:46,630 --> 00:04:52,210 It would help make sure that I always write out custom errors with the correct structure or status code 72 00:04:52,300 --> 00:04:57,900 and serialize errors so again this is an absolutely viable option but it's not quite what we're gonna 73 00:04:57,910 --> 00:04:59,030 do this time around. 74 00:04:59,110 --> 00:05:04,700 So I'm going to delete that interface and implements on the very end as well. 75 00:05:07,140 --> 00:05:08,380 So what are we gonna do instead. 76 00:05:08,750 --> 00:05:12,890 Well we're going to take a very similar approach but instead of using an interface we're gonna do something 77 00:05:12,950 --> 00:05:14,630 slightly different. 78 00:05:14,630 --> 00:05:19,910 We're going to create a new abstract class called customer the abstract class is going to serve the 79 00:05:19,910 --> 00:05:23,000 exact same purpose as the interface that we just looked at. 80 00:05:23,000 --> 00:05:27,620 So it's going to set out a number of properties that must be defined in order to be considered to be 81 00:05:27,620 --> 00:05:28,850 a custom error. 82 00:05:29,120 --> 00:05:34,640 If you're not familiar with abstract classes in general and typescript couple of reminders on them an 83 00:05:34,640 --> 00:05:40,750 abstract class is like a class but it can not be directly instantiated in other words we cannot say 84 00:05:40,820 --> 00:05:47,660 new custom air instead custom or semi abstract classes are used to organize our code and somehow setup 85 00:05:47,720 --> 00:05:51,060 requirements or subclasses of this class. 86 00:05:51,080 --> 00:05:56,520 So really it's being used to set up a requirement for a subclass. 87 00:05:56,530 --> 00:06:01,180 The nice thing about a abstract class is that when we translate this over to javascript to actually 88 00:06:01,180 --> 00:06:07,750 execute we actually do end up with a class definition when we translate interfaces to JavaScript all 89 00:06:07,750 --> 00:06:09,390 interfaces fall away. 90 00:06:09,400 --> 00:06:14,490 They don't actually exist in the world of javascript but abstract classes do. 91 00:06:14,650 --> 00:06:20,920 And that means that we can use an abstract class with an instance of check and that's extremely relevant 92 00:06:20,950 --> 00:06:28,050 because remember back over inside of our area handling middleware it is right here we are doing some 93 00:06:28,050 --> 00:06:31,940 instance of checks if we continue with the same pattern that we have right here. 94 00:06:32,000 --> 00:06:37,890 We would eventually have to have a ton of different if statements for all the different kinds of errors 95 00:06:38,220 --> 00:06:41,970 and so we start to run into that issue again where are error handling middleware starts to get really 96 00:06:41,970 --> 00:06:51,080 bloated but if all these custom errors are going to be extending some custom based class of customer 97 00:06:51,080 --> 00:06:55,790 right here we can instead just have one single instance of checks something that says is the error an 98 00:06:55,790 --> 00:07:02,630 instance of custom error subclasses of this custom air abstract class will satisfy this instance of 99 00:07:02,630 --> 00:07:03,340 check. 100 00:07:03,340 --> 00:07:07,880 So we will only have to write out that if statement one time and that will capture all possible customer 101 00:07:07,880 --> 00:07:10,880 errors that ever get thrown inside our application. 102 00:07:10,880 --> 00:07:15,980 So long story short rather than defining an interface and having our classes implemented we're going 103 00:07:15,980 --> 00:07:22,880 to instead define a abstract class called customer and we're going to have request validation and database 104 00:07:22,880 --> 00:07:23,860 connection error. 105 00:07:24,050 --> 00:07:30,710 Extend that abstract class Oh I know this stuff is nasty isn't it. 106 00:07:30,750 --> 00:07:35,010 This is why learning this stuff this is why you go and get a job and get paid the big bucks is because 107 00:07:35,010 --> 00:07:37,100 this stuff is just ridiculous sometimes. 108 00:07:37,140 --> 00:07:41,760 I know this is just really challenging stuff right now and I know it's a lot of typescript stuff and 109 00:07:41,760 --> 00:07:47,130 it's not really specific to the world of micros services but this is stuff where again you gotta make 110 00:07:47,130 --> 00:07:49,410 sure same thing this is all about. 111 00:07:49,530 --> 00:07:54,660 We have to make sure that the air's coming out of our services are always satisfying that same structure 112 00:07:55,200 --> 00:08:00,420 and it is by doing stuff like this right here literally exactly this that we're going to make sure that 113 00:08:00,570 --> 00:08:05,370 all the different errors we ever create inside our application have that identical structure. 114 00:08:05,490 --> 00:08:09,690 If you tried doing all this stuff with javascript I can guarantee you you would start to run into errors 115 00:08:09,720 --> 00:08:14,970 almost immediately and I say errors I mean bad errors not these actual handle errors that we're talking 116 00:08:14,970 --> 00:08:20,810 about know this typescript stuff is nasty but this is how we implement structure inside of really large 117 00:08:20,810 --> 00:08:26,570 code bases it's just how it's done and it's what we have to do to implement micro services in some reasonable 118 00:08:26,570 --> 00:08:27,070 fashion. 119 00:08:27,740 --> 00:08:27,980 OK. 120 00:08:28,010 --> 00:08:30,350 So a little bit of a spontaneous pep talk there anyways. 121 00:08:30,360 --> 00:08:31,190 Let's take a pause right here. 122 00:08:31,190 --> 00:08:34,160 We're gonna come back next video start to implement this abstract class.