1
00:00:01,270 --> 00:00:02,410
In this video.

2
00:00:02,410 --> 00:00:07,480
Let's see how we can handle what is called on handle rejection.

3
00:00:07,930 --> 00:00:16,500
An example of this kind of error is when the DB connection refused to connect to MongoDB, meaning if

4
00:00:16,510 --> 00:00:22,840
there are some connection error to MongoDB and then another type of error that it can be OC and the

5
00:00:22,870 --> 00:00:31,240
on handle rejection is that if we refuse to bring in catch block inside async await function and this

6
00:00:31,240 --> 00:00:35,110
kind of error is going to help us in the development mode.

7
00:00:35,140 --> 00:00:40,210
Let me show you an example inside the config file for the DB.

8
00:00:40,240 --> 00:00:49,150
Let's change the password and for this one if I check my terminal, you can see that we have some random

9
00:00:49,150 --> 00:00:51,040
string of error here.

10
00:00:51,160 --> 00:00:57,190
And again, if you are new to programming or especially to MongoDB, this kind of error message is,

11
00:00:57,190 --> 00:00:58,680
you know, be understand.

12
00:00:58,690 --> 00:01:06,460
So to be able to send the required or simplified message, we are going to handle this kind of can handle

13
00:01:06,460 --> 00:01:07,930
promise rejection.

14
00:01:07,960 --> 00:01:15,500
So how are we going to do it so we inside the server file, we need to create an instance of the server.

15
00:01:15,520 --> 00:01:23,320
So here we are going to assign a variable called server to what comes back from the app dot.

16
00:01:23,390 --> 00:01:24,040
Listen.

17
00:01:24,190 --> 00:01:28,980
And on this one we can change or listen to any event type.

18
00:01:28,990 --> 00:01:37,390
Remember, Node.js is an event driven technology, so it means that we can listen to types of events

19
00:01:37,390 --> 00:01:43,810
and the first type of event that we are going to listen to is what we call on handle rejection and we

20
00:01:43,810 --> 00:01:47,080
have access to that on the process dot on.

21
00:01:47,080 --> 00:01:54,670
And here we are listening to an event types and we have different types of events in Node.js.

22
00:01:54,670 --> 00:02:02,680
The one that we are going to handle is what we call on handle rejection, which is this one in our pass

23
00:02:02,680 --> 00:02:04,390
in a callback function.

24
00:02:04,390 --> 00:02:08,470
I have access to the actual error and then the promise.

25
00:02:08,470 --> 00:02:10,660
So here we go.

26
00:02:10,840 --> 00:02:16,540
So here I can go ahead and then console.log this simplified message.

27
00:02:16,570 --> 00:02:23,830
If you check the console, you can see that we have the message as back off, which is the authentication

28
00:02:23,830 --> 00:02:27,550
error, meaning that I pass a wrong password.

29
00:02:27,550 --> 00:02:35,260
So here we have access to those messages on error message and error dot name.

30
00:02:35,260 --> 00:02:42,370
So here you go back tick and then dollar sign replaces the error dot name.

31
00:02:43,740 --> 00:02:52,500
And then space dollar sign carry braces, error, dot message and that is it.

32
00:02:52,500 --> 00:02:54,000
So let's save it.

33
00:02:54,000 --> 00:03:01,380
And now let's check the console and you can see that we got simplify message as mongo server error but

34
00:03:01,380 --> 00:03:04,470
authentication meaning the authentication failed.

35
00:03:04,650 --> 00:03:05,160
All right.

36
00:03:05,160 --> 00:03:11,580
So here ends the section on how we can handle errors in experts charges.

