1
00:00:00,570 --> 00:00:01,770
In this video.

2
00:00:01,770 --> 00:00:06,960
Let's get started with how we can handle errors in our application.

3
00:00:07,170 --> 00:00:14,940
And the first type of error that we are going to handle is called 404 error, which means that if a

4
00:00:14,940 --> 00:00:22,410
user is trying to access an endpoint which does not exist in our application, we need to throw a beautiful

5
00:00:22,410 --> 00:00:23,700
error to the user.

6
00:00:24,030 --> 00:00:30,720
So before we continue, I have provided a sample express server with few lines of code.

7
00:00:30,720 --> 00:00:38,670
So if you open the server file, you can see that we have only one endpoint that sends a message that

8
00:00:38,670 --> 00:00:40,530
we come to error handling.

9
00:00:40,530 --> 00:00:49,560
And I have started the server down here and then inside the config folder, this file contains a function

10
00:00:49,560 --> 00:00:56,730
to connect to MongoDB and I have provided a connection string for you, but I advise you to use your

11
00:00:56,730 --> 00:01:01,500
own connection string because I might change the password later on.

12
00:01:01,500 --> 00:01:07,260
And in case you find difficult to connect, it can happen that I have changed my connection string.

13
00:01:07,560 --> 00:01:13,860
The purpose for this function is I will show you how we can handle mongoose errors as well.

14
00:01:14,250 --> 00:01:17,490
And the next folder is model folder.

15
00:01:17,490 --> 00:01:23,460
And for this one too, I will show you how we can handle errors when we are creating a post and save

16
00:01:23,460 --> 00:01:25,650
it in case something goes wrong.

17
00:01:26,190 --> 00:01:30,750
The next step is make sure that your server is running.

18
00:01:30,780 --> 00:01:31,380
All right.

19
00:01:31,380 --> 00:01:34,830
Now let's get started with the error handling.

20
00:01:34,830 --> 00:01:41,880
Well, before we continue, guys, you can use Postman to test your endpoint or you can use the built

21
00:01:41,880 --> 00:01:44,250
in extension core tender.

22
00:01:44,250 --> 00:01:47,640
If it's set for tender, this is what you're going to see.

23
00:01:47,860 --> 00:01:53,490
Install it and then you can make a request right from your Visual Studio code.

24
00:01:53,490 --> 00:02:04,290
So let's go ahead and create one request and that is HTTP colon for a slash for slash look, Host colon

25
00:02:04,290 --> 00:02:06,480
3000 and let's get sent.

26
00:02:06,480 --> 00:02:12,930
And you can see I have the response for my home root, which is this one.

27
00:02:13,410 --> 00:02:13,740
All right.

28
00:02:13,740 --> 00:02:18,000
Let's say we are trying to access an endpoint that does not exist.

29
00:02:18,000 --> 00:02:19,290
So let's see how I'm going to do it.

30
00:02:19,290 --> 00:02:21,720
I can pass in, for example, log in.

31
00:02:22,020 --> 00:02:26,040
Remember, this root does not exist in our application.

32
00:02:26,040 --> 00:02:34,470
So if I had sent you can see that Xpress has through an error which is called 404, meaning this endpoint

33
00:02:34,470 --> 00:02:43,470
does not exist and it has sent us an HTML which says that cannot get last log in even though express

34
00:02:43,470 --> 00:02:44,700
is through an error.

35
00:02:44,700 --> 00:02:47,040
But this is not all that friendly.

36
00:02:47,040 --> 00:02:53,280
In case we send this kind of error to the end user, that person might not know what is going on.

37
00:02:53,280 --> 00:02:57,960
In that case, we need to handle and display some beautiful error to the user.

38
00:02:57,960 --> 00:02:59,970
So how can we handle that?

39
00:02:59,970 --> 00:03:01,890
So back to the server file.

40
00:03:01,920 --> 00:03:03,330
Here we go.

41
00:03:03,450 --> 00:03:05,220
Take note about this.

42
00:03:05,250 --> 00:03:09,420
All error handles must come below your roots.

43
00:03:09,420 --> 00:03:13,710
So here is a function to handle foo or for error.

44
00:03:13,890 --> 00:03:24,930
So on the app we have a method called app dot or simply means that it can catch any endpoint, whether

45
00:03:24,930 --> 00:03:29,610
it is post request, delete updates or patch.

46
00:03:29,610 --> 00:03:37,050
So for this one, we're going to pass in a universal endpoint going to be called and then asterisk as

47
00:03:37,050 --> 00:03:40,530
that the next argument going to be a callback function.

48
00:03:40,530 --> 00:03:42,540
We have access to requests.

49
00:03:43,970 --> 00:03:44,930
Response.

50
00:03:44,930 --> 00:03:46,610
And then next.

51
00:03:46,790 --> 00:03:54,560
Remember, for these handlers, too, we can pass in three agreements as that, but most of times we

52
00:03:54,560 --> 00:03:56,570
ignore the next.

53
00:03:56,660 --> 00:04:02,330
But I will show you why you may need next when you are trying to handle arrows inside out.

54
00:04:02,510 --> 00:04:08,320
For the meantime, let's focus on how we can handle four or four errors.

55
00:04:08,330 --> 00:04:10,910
So now I'm going to use a function.

56
00:04:11,810 --> 00:04:13,040
Here we go.

57
00:04:13,160 --> 00:04:17,779
So here I'm going to send a message in case there is one.

58
00:04:17,779 --> 00:04:25,550
So I'm going to send a message with status code and for a root that does not exist, the status code

59
00:04:25,550 --> 00:04:32,000
is always 404 and then I want to convert everything to JSON instead of just a HTML.

60
00:04:32,030 --> 00:04:37,370
And here I can provide any property and I will say that route.

61
00:04:38,090 --> 00:04:38,960
No, it's.

62
00:04:40,040 --> 00:04:41,030
Found.

63
00:04:41,330 --> 00:04:44,300
Now, let's explain what is going on here.

64
00:04:44,480 --> 00:04:49,160
You can see that we have first handler, which is the home route.

65
00:04:49,370 --> 00:04:55,430
So in case there are some error, for example, there is no route that does not exist in replication,

66
00:04:55,430 --> 00:05:00,770
then going to skip all these ones and then move on to this catch all.

67
00:05:00,800 --> 00:05:05,430
So let's see in action when I save this one and I make request.

68
00:05:05,450 --> 00:05:10,040
Now you can see that I get what is called route not found.

69
00:05:10,040 --> 00:05:13,130
So we're going to be e here typing mistake.

70
00:05:13,220 --> 00:05:21,440
So again, when you run our server, first of all is trying to locate is there any route with something

71
00:05:21,440 --> 00:05:22,950
like forward slash?

72
00:05:22,970 --> 00:05:23,660
No.

73
00:05:23,690 --> 00:05:26,600
Then it will fall to the cut off.

74
00:05:26,630 --> 00:05:28,340
That's why we got it.

75
00:05:28,340 --> 00:05:29,120
As what?

76
00:05:29,150 --> 00:05:29,880
This.

77
00:05:29,900 --> 00:05:33,260
So that's about the 404 error handling.

78
00:05:33,470 --> 00:05:36,460
But let's say that we are making a request to the home page.

79
00:05:36,470 --> 00:05:37,360
Now, let's see.

80
00:05:37,370 --> 00:05:44,430
You can see that we have the message, which means that this particular route exists in that way.

81
00:05:44,450 --> 00:05:46,690
It will never get to this point.

82
00:05:46,700 --> 00:05:51,130
So any time a route runs, it means that it exists in that way.

83
00:05:51,140 --> 00:05:54,110
It will not come here to execute.

84
00:05:54,200 --> 00:05:58,550
So this is how we can handle such error.

85
00:05:58,700 --> 00:06:02,780
But for this error too, we can send more detailed message.

86
00:06:02,780 --> 00:06:09,020
For example, if a user is trying to access something like log in.

87
00:06:09,730 --> 00:06:16,510
When I hit saying, You want to say that first last login does not exist, so how are you going to do

88
00:06:16,510 --> 00:06:16,780
it?

89
00:06:16,810 --> 00:06:21,100
So inside this handler, we have access to the original.

90
00:06:21,100 --> 00:06:28,180
You are on the request object, so if you consider log rec.

91
00:06:29,600 --> 00:06:31,820
Dot's original u r l.

92
00:06:31,850 --> 00:06:34,490
Let's save it and let's see the response.

93
00:06:34,490 --> 00:06:37,520
So now back to the request when I hit send.

94
00:06:37,550 --> 00:06:38,540
Now have a look.

95
00:06:38,540 --> 00:06:46,010
You can see that I have four slots, so we can say that four slots does not exist or not found.

96
00:06:46,010 --> 00:06:48,740
So let's go ahead and send that message.

97
00:06:48,740 --> 00:06:59,780
So inside the handler here, let me use back take dollar sign and then the requests Dot's original u

98
00:06:59,780 --> 00:07:02,900
URL L And here I will say not.

99
00:07:03,470 --> 00:07:04,430
Found.

100
00:07:04,520 --> 00:07:06,230
All right, so now let's see.

101
00:07:06,710 --> 00:07:09,110
Let's make the requests.

102
00:07:09,110 --> 00:07:17,390
And I can see that for slots not found or when I make use of register, you're going to see that register

103
00:07:17,420 --> 00:07:18,820
not found.

104
00:07:18,830 --> 00:07:23,120
So this is how we can handle four or four errors.

105
00:07:23,120 --> 00:07:27,980
So what about errors that might work inside our root?

106
00:07:28,160 --> 00:07:30,800
So this one is outside our roots.

107
00:07:30,800 --> 00:07:32,990
And what about inside our roots?

108
00:07:32,990 --> 00:07:35,630
How are we going to handle such errors?

109
00:07:35,630 --> 00:07:37,910
Let's get into that in the next video.

