1
00:00:00,200 --> 00:00:03,710
So what I want to do now is create a custom error handler.

2
00:00:03,710 --> 00:00:09,110
So whenever we have any errors, whether there's, you know, there's product we're trying to find that

3
00:00:09,110 --> 00:00:16,820
doesn't match the ID or whatever, it always it always shows the same thing, an object with a message

4
00:00:16,820 --> 00:00:19,250
and also the stack trace.

5
00:00:19,250 --> 00:00:24,740
If we're in development and you can you can have your errors formatted absolutely however you want.

6
00:00:24,740 --> 00:00:26,650
But I want to just keep it simple.

7
00:00:26,660 --> 00:00:32,840
Now in Express, there's a default error handler and if you want to overwrite it, you can do that.

8
00:00:33,110 --> 00:00:42,530
Let's see by just creating this a function called error handler and pass in error as the first argument

9
00:00:42,530 --> 00:00:43,070
here.

10
00:00:43,070 --> 00:00:48,770
And then you can do whatever you want as far as statuses and messages and all that.

11
00:00:49,010 --> 00:00:54,440
Now, what we did with the async handler is actually an example right here.

12
00:00:54,650 --> 00:00:56,300
So see this right here?

13
00:00:56,300 --> 00:01:00,390
This this promise resolve dot then.

14
00:01:00,390 --> 00:01:04,920
And basically this is what we did with the async handler.

15
00:01:04,920 --> 00:01:12,150
So this will avoid the overhead of try catch blocks when using functions that return promises.

16
00:01:12,150 --> 00:01:14,910
So that's why we added that async handler.

17
00:01:14,910 --> 00:01:19,470
But what we're going to do is create let's just close this up for now.

18
00:01:19,650 --> 00:01:22,980
Let's create in our back end middleware folder.

19
00:01:22,980 --> 00:01:25,410
Let's create a file called Error.

20
00:01:26,340 --> 00:01:28,010
Middleware dot.

21
00:01:28,050 --> 00:01:33,010
JS And there's basically two functions that I want to create here.

22
00:01:33,030 --> 00:01:39,600
The first one is going to be called NotFound, and basically this will this will be called if no other

23
00:01:39,600 --> 00:01:46,110
middleware has handled the request and it will create a new error object and set the code to 404, which

24
00:01:46,110 --> 00:01:47,820
is a not found error.

25
00:01:48,090 --> 00:01:48,420
All right.

26
00:01:48,420 --> 00:01:53,730
So basically, we're going to create a variable, set it to new error, and we're going to pass in a

27
00:01:53,730 --> 00:02:00,000
message of not found dash and then the original URL, which we can get from the request object.

28
00:02:00,000 --> 00:02:06,700
And then we set the status to 404 and we call the next piece of middleware passing in that error variable.

29
00:02:06,720 --> 00:02:14,520
Now to overwrite the default express handler error handler, we're going to create a function called

30
00:02:14,520 --> 00:02:15,690
error handler.

31
00:02:16,440 --> 00:02:16,760
All right.

32
00:02:16,800 --> 00:02:23,430
And then and that gets passed in not only requests response next, but also error at the beginning.

33
00:02:23,670 --> 00:02:27,810
So I'm going to initialize a couple variables here.

34
00:02:27,810 --> 00:02:33,290
So let's say status or status code, and I'm going to set that to a ternary.

35
00:02:33,300 --> 00:02:40,080
So we're going to check to see if the status code is 200, because it may be if we're throwing an error,

36
00:02:40,080 --> 00:02:48,990
we want to be able to throw an error from anywhere by doing this, say throw new error and then pass

37
00:02:48,990 --> 00:02:50,130
in some message.

38
00:02:50,130 --> 00:02:51,480
So we want to be able to do that.

39
00:02:51,480 --> 00:02:55,440
And if it's 200, I want it to change to 500.

40
00:02:55,650 --> 00:02:55,980
Okay.

41
00:02:55,980 --> 00:03:01,310
If it's anything else like 404 or whatever, then that's what it's going to stay.

42
00:03:01,320 --> 00:03:01,620
Okay.

43
00:03:01,620 --> 00:03:03,990
So it'll be whatever that status code is.

44
00:03:03,990 --> 00:03:06,370
So if it's 200, it'll be 500.

45
00:03:06,390 --> 00:03:09,900
If it's something different, it'll stay whatever it is.

46
00:03:10,080 --> 00:03:15,930
Then for the message, let's say let message equals and then error message.

47
00:03:15,930 --> 00:03:20,760
So if I say throw new error and I pass in a message, we can access it from here.

48
00:03:20,760 --> 00:03:23,220
And that's what we're setting to this variable.

49
00:03:23,400 --> 00:03:33,220
Now I want to check for let's say, yeah, we can say that mongoose bad object ID or cast error.

50
00:03:33,220 --> 00:03:36,430
And I showed you an example of that in the last video.

51
00:03:36,430 --> 00:03:44,560
If we get a single product and we pass in an ID that doesn't exist, it gives us this HTML page with

52
00:03:44,560 --> 00:03:47,110
a 500 status.

53
00:03:47,110 --> 00:03:54,430
And if this is a cast error, so I want to check for this and show something different if that does

54
00:03:54,430 --> 00:03:55,270
happen.

55
00:03:55,270 --> 00:03:59,110
So we need to first of all, we're going to say if.

56
00:03:59,880 --> 00:04:00,690
Yeah, we can do that.

57
00:04:00,690 --> 00:04:01,710
So if.

58
00:04:02,650 --> 00:04:03,370
Error.

59
00:04:05,430 --> 00:04:06,270
I'll just type it out.

60
00:04:06,270 --> 00:04:10,440
So if the error name is equal to cast.

61
00:04:11,530 --> 00:04:17,709
Error, but I also want to check the kind so there's this kind value on the error.

62
00:04:17,740 --> 00:04:18,519
Object.

63
00:04:18,519 --> 00:04:21,370
And we want to check to see if its object ID.

64
00:04:21,820 --> 00:04:29,110
If so, then we're going to set the message that we initialized up here to resource not found and we're

65
00:04:29,110 --> 00:04:31,930
going to set that status code to 404.

66
00:04:32,260 --> 00:04:32,650
Okay.

67
00:04:32,650 --> 00:04:38,290
Then we want to just do our final response, which is going to be, let's say res dot status.

68
00:04:39,120 --> 00:04:42,980
And we're going to pass in whatever that status code is.

69
00:04:42,990 --> 00:04:51,450
And then the Json, which is just going to be basically an object with the message.

70
00:04:52,210 --> 00:04:55,210
From the message variable, so we can just keep it like that.

71
00:04:55,210 --> 00:05:02,980
And then I'm also going to send the stack trace, which can be helpful for developers if we're in development.

72
00:05:02,980 --> 00:05:09,880
So if it's in production, it's just going to send this this pancakes and a stack of pancakes else.

73
00:05:09,880 --> 00:05:14,170
Then it's going to send the actual stack to stack trace.

74
00:05:14,770 --> 00:05:17,320
All right, then we just want to export these.

75
00:05:17,320 --> 00:05:21,160
So we'll say export not found an error handler.

76
00:05:21,160 --> 00:05:27,850
And then to use these, we have to go into our server.js and first of all, bring them in.

77
00:05:27,850 --> 00:05:35,590
Let's say import not found an error handler from middleware slash error middleware.

78
00:05:35,620 --> 00:05:44,200
JS And then we're going to go down here under all the routes and say app dot use not found and app dot

79
00:05:44,200 --> 00:05:45,700
use error handler.

80
00:05:45,700 --> 00:05:52,880
So now what I'm going to do is in our product route where we test to see if there's a product, we respond

81
00:05:52,880 --> 00:05:59,510
with it else instead of doing this, which isn't even working because that cast error is is taking control.

82
00:05:59,510 --> 00:06:05,540
Instead of doing that, I'm going to use our new error handler by first setting the status to 404 and

83
00:06:05,540 --> 00:06:11,060
then throwing a new error and just saying product not found or, let's say resource.

84
00:06:12,310 --> 00:06:13,930
Resource not found.

85
00:06:13,930 --> 00:06:19,540
So now let's come back over here and let's get the product with one with the ID of one which doesn't

86
00:06:19,540 --> 00:06:20,530
exist.

87
00:06:20,530 --> 00:06:23,620
And now you can see we get a 404 response.

88
00:06:23,650 --> 00:06:27,760
We get a message resource not found, and then we get the stack trace.

89
00:06:27,760 --> 00:06:32,380
So no more HTML page, which doesn't make sense for a Json API.

90
00:06:33,720 --> 00:06:34,050
Okay.

91
00:06:34,050 --> 00:06:40,350
And if we want to throw any error, like let's go to our products route and just say throw.

92
00:06:41,350 --> 00:06:45,280
New error and just say some error.

93
00:06:45,280 --> 00:06:52,420
So now if I go to my products, get all products and I send we get a 500 because we didn't set it to

94
00:06:52,420 --> 00:06:53,170
anything else.

95
00:06:53,170 --> 00:07:00,400
Remember, if it's if it's 200, it just gets sets to 500 some error and then we get our stack trace.

96
00:07:01,280 --> 00:07:07,550
Okay, so that little bit of code that we added makes things much more elegant when it comes to handling

97
00:07:07,550 --> 00:07:08,000
errors.

98
00:07:08,000 --> 00:07:09,770
We just do throw new error.

99
00:07:09,800 --> 00:07:13,610
We can set a status right before it and we're all good.

100
00:07:15,210 --> 00:07:15,570
All right.

101
00:07:15,570 --> 00:07:21,750
So the last thing I want to do on the back end before we move back to the front end is create controllers.

102
00:07:21,750 --> 00:07:27,990
Instead of putting all of our logic in the root file, we're going to have a controllers for products,

103
00:07:27,990 --> 00:07:31,830
users and orders and put all of our functionality there.

