WEBVTT

1
00:00.980 --> 00:02.630
Hi everyone.

2
00:04.130 --> 00:12.140
In this video let me show you the core concept of Nest.js will be exception filters.

3
00:12.170 --> 00:12.650
Okay.

4
00:12.680 --> 00:15.680
Click on the exception filters right here.

5
00:16.670 --> 00:19.430
And where what is the exception filter?

6
00:21.770 --> 00:33.440
If you already built the API inside the expressjs framework we use the custom the error by hand,

7
00:33.470 --> 00:34.010
right?

8
00:34.040 --> 00:42.260
For example like bad request exception or unauthorized exception or internal exception.

9
00:42.260 --> 00:42.890
Right.

10
00:44.390 --> 00:55.220
But with but with NestJS, it's already have a some exceptions okay.

11
00:55.250 --> 01:01.100
By default it will be used the exception which will be the internal server.

12
01:03.950 --> 01:04.520
Okay.

13
01:04.730 --> 01:07.190
It also provides something like this.

14
01:08.090 --> 01:10.250
This is the global exception filter.

15
01:13.430 --> 01:20.060
And let me show you how we can throw the exceptions.

16
01:22.190 --> 01:22.580
Okay.

17
01:22.610 --> 01:27.920
Nestjs have a HTTP exception inside the nestjs/common.

18
01:28.820 --> 01:29.120
Okay.

19
01:29.120 --> 01:30.710
We can throw something like this.

20
01:30.740 --> 01:32.150
Let me show you.

21
01:33.530 --> 01:36.770
For example, I have a service right here.

22
01:36.800 --> 01:43.340
I get the data, but before I get the data, I will throw new.

23
01:44.450 --> 01:49.040
Let me see, HTTP exception, that it will be exception.

24
01:51.200 --> 01:51.590
Okay.

25
01:51.710 --> 01:53.390
We can use for with that.

26
01:54.590 --> 01:57.410
Let me be forbidden.

27
01:58.340 --> 02:01.250
And the status?

28
02:01.280 --> 02:03.860
The status is already had.

29
02:03.860 --> 02:04.310
Status.

30
02:04.340 --> 02:06.170
Inside the HTTP status.

31
02:07.160 --> 02:09.530
Constant status.

32
02:09.710 --> 02:11.200
from common right here.

33
02:11.980 --> 02:12.520
Okay.

34
02:13.390 --> 02:17.110
And .FORBIDDEN

35
02:17.260 --> 02:17.800
Like this.

36
02:18.130 --> 02:24.250
And I think I will open the postman to test this.

37
02:41.410 --> 02:48.460
Let me create and I will close the other tab.

38
02:52.360 --> 02:52.720
Okay.

39
02:52.750 --> 02:53.770
I use get method.

40
02:53.770 --> 02:56.620
And I will copy the endpoint here.

41
02:57.460 --> 03:05.230
Paste it, and if I send request, now you will see it has a forbidden http status code.

42
03:05.620 --> 03:07.840
And we some message right here.

43
03:08.110 --> 03:08.770
Very cool.

44
03:08.800 --> 03:09.460
Right.

45
03:14.830 --> 03:15.670
Now

46
03:15.700 --> 03:21.670
This here will be a

47
03:21.700 --> 03:22.150
Right here.

48
03:22.150 --> 03:23.620
I can change the message.

49
03:23.860 --> 03:25.180
For example.

50
03:29.740 --> 03:30.250
This one.

51
03:32.590 --> 03:39.610
Okay, let's see, if I send okay, we have the message, right?

52
03:40.150 --> 03:40.840
Very good.

53
03:43.090 --> 03:53.680
And, we can custom that, for example, I don't want the throw with the some arguments like this.

54
03:53.680 --> 03:59.470
I can easily create the custom exception.

55
04:01.960 --> 04:03.280
For exception.

56
04:04.570 --> 04:05.020
Okay.

57
04:05.050 --> 04:13.300
I can create a class called forbidden exception.

58
04:13.300 --> 04:15.580
And let me copy everything right here.

59
04:15.970 --> 04:17.440
Paste it's okay.

60
04:17.470 --> 04:18.370
Export a class.

61
04:18.370 --> 04:23.890
And make sure we will inherit from at the http exception.

62
04:24.340 --> 04:26.830
And import that okay.

63
04:26.860 --> 04:28.900
And right now, how we can use that.

64
04:28.930 --> 04:36.910
We can easily throw forbidden exception, from my exception

65
04:37.000 --> 04:37.570
Okay.

66
04:40.780 --> 04:44.020
Now, let me see, I can send.

67
04:45.190 --> 04:46.810
And it also work, right?

68
04:49.990 --> 04:50.560
Okay.

69
04:50.590 --> 04:51.910
Here is a video.

70
04:52.300 --> 04:59.020
And actually in the nestjs, it's already built-in the some exception for me.

71
04:59.050 --> 05:04.990
For example, like bad exception, unauthorized exception, not found forbidden.

72
05:05.620 --> 05:08.500
Uh, and many more.

73
05:09.100 --> 05:10.630
We can easily do that.

74
05:11.080 --> 05:20.020
For example, I can throw new BadRequestException with something like.

75
05:20.050 --> 05:22.930
Data not found, for example.

76
05:22.930 --> 05:24.180
Like this

77
05:32.130 --> 05:43.770
And if I send, you can see, it will have a message data not found and error and the 400

78
05:45.240 --> 05:45.870
Very cool.

79
05:45.900 --> 05:46.500
Right.

80
05:50.550 --> 05:55.560
So I think that's for the video.

81
05:58.410 --> 05:59.130
Let me show see.

82
06:00.630 --> 06:01.170
Okay.

83
06:02.310 --> 06:03.510
We have many more.

84
06:04.500 --> 06:05.550
And it have an internal server error.

85
06:05.790 --> 06:08.070
Internal server error reception.

86
06:08.460 --> 06:09.120
Very cool.

87
06:09.240 --> 06:13.650
And for that reason we doesn't need to custom.

88
06:14.370 --> 06:14.850
Okay.

89
06:15.090 --> 06:23.160
But if you want to custom you can you feel free to use in your case.

90
06:23.640 --> 06:24.210
Okay.

91
06:25.140 --> 06:26.670
And that's for the video.

92
06:26.700 --> 06:28.590
I will see you in the next one.