1
00:00:00,330 --> 00:00:07,230
Beautiful, before we worry about deployment, let's make our Mangoush, our responses more user friendly,

2
00:00:07,680 --> 00:00:13,860
and before we start, let me just say that just like our air classes, we will only do this once.

3
00:00:14,220 --> 00:00:17,390
And after that, we'll bring it from project to project.

4
00:00:17,730 --> 00:00:23,040
So if at some point you get bored, just remember, we only need to do that in this project.

5
00:00:23,220 --> 00:00:27,140
And then for a mining project, of course, we'll just reuse the code.

6
00:00:27,570 --> 00:00:31,530
And the idea is following where currently we have three Mangoush errors.

7
00:00:31,530 --> 00:00:32,460
We have validation.

8
00:00:32,460 --> 00:00:39,360
One member of that is if the user doesn't provide one of the values, then we have the duplicate issue

9
00:00:39,630 --> 00:00:41,190
where email is unique.

10
00:00:41,190 --> 00:00:43,050
And of course, we also have the Kastor.

11
00:00:43,320 --> 00:00:50,420
And if you remember, that's when the syntax doesn't match exactly to what the Mangoush is looking for.

12
00:00:50,850 --> 00:00:55,560
We want to go to the middleware and more specifically, we want to go to the area handler.

13
00:00:55,860 --> 00:01:03,540
And of course, at the moment I'm logging the error and in here I'm just sending back this generic response.

14
00:01:03,690 --> 00:01:07,470
So internal server error and I'm just passing in the object.

15
00:01:07,800 --> 00:01:12,480
And if I take a look at the postman, I'm going to be looking for a register out.

16
00:01:12,720 --> 00:01:21,120
And then if I have already a user in this example, Susan, so if I already have that email and if I

17
00:01:21,120 --> 00:01:26,880
try to register one more time, of course, I'm going to get that duplicate error correct, because

18
00:01:26,880 --> 00:01:29,760
I already have the email in the system.

19
00:01:30,120 --> 00:01:35,490
And essentially the goal is following where we want to send back more user friendly response.

20
00:01:35,850 --> 00:01:40,350
And also we want to change the status code because at the moment we're just sending five hundred.

21
00:01:40,360 --> 00:01:42,500
But of course, this is for her.

22
00:01:42,930 --> 00:01:44,310
This is a bad request.

23
00:01:44,790 --> 00:01:52,260
And the way we do that, we navigate back to our handler Mador, and we'll start by creating a object,

24
00:01:52,590 --> 00:01:54,510
a custom or object.

25
00:01:54,780 --> 00:01:59,360
And the reason we will do that, because we'll set up multiple if statements.

26
00:01:59,790 --> 00:02:06,390
Now, technically, you can also manipulate this code and I'll show you in a second why and how.

27
00:02:06,780 --> 00:02:09,020
But in my case, I'll just leave it the way it is.

28
00:02:09,419 --> 00:02:16,260
So if our error is our custom one, basically, if it's one of the classes, then we'll just write a

29
00:02:16,260 --> 00:02:17,400
Western backed response.

30
00:02:17,730 --> 00:02:26,340
If not, there's going to be a new customer object and then as far as the response will send back that

31
00:02:26,340 --> 00:02:27,210
object instead.

32
00:02:27,570 --> 00:02:29,190
So let's start working on that.

33
00:02:29,190 --> 00:02:32,210
And essentially, we want to go with some kind of variable.

34
00:02:32,480 --> 00:02:38,040
I guess I'm going to go with let just so I know that I'm going to be manipulating those values and I'll

35
00:02:38,040 --> 00:02:43,790
set custom error and that one is equal to status code and the message.

36
00:02:44,040 --> 00:02:48,420
So here I want to set up two properties and basically you want to set defaults.

37
00:02:48,780 --> 00:02:49,800
So the same idea.

38
00:02:50,160 --> 00:02:53,430
If there is already something provided, then of course we'll send back that.

39
00:02:53,850 --> 00:02:56,850
If not, then there's going to be some kind of default.

40
00:02:57,240 --> 00:03:00,720
And effectively it's going to look something like this where we'll go with the property name.

41
00:03:00,960 --> 00:03:07,200
So status code and this will be equal to error and then the status code.

42
00:03:07,530 --> 00:03:12,890
And eventually there's going to be multiple if statements that will manipulate this value.

43
00:03:13,110 --> 00:03:14,790
So in here, I'm just setting up the default.

44
00:03:15,030 --> 00:03:20,760
I say, hey, listen, if in the error I already have the status quo, then use that.

45
00:03:20,970 --> 00:03:27,030
If not, well, then let's set up the generic response, let's say status codes, and we're looking

46
00:03:27,030 --> 00:03:29,250
for internal server error.

47
00:03:29,850 --> 00:03:31,860
And that, of course, is going to be five hundred.

48
00:03:32,100 --> 00:03:36,330
And let's do the same thing with the message, the message, property and error.

49
00:03:36,540 --> 00:03:42,840
And let's just say your message and let's go to vertical bars and we'll say something.

50
00:03:43,590 --> 00:03:50,640
Something went wrong and let's say try again, again later.

51
00:03:51,000 --> 00:03:56,820
And now, of course, what we want to do is change this code around where instead of sending this internal

52
00:03:56,820 --> 00:04:01,620
server error and Jason, an error, of course, we'll use these values instead.

53
00:04:01,950 --> 00:04:06,000
Now, I will still leave this code, meaning I'll just comment this out.

54
00:04:06,010 --> 00:04:11,880
So copy and paste and coming down just because I'll be sending those error object back to the postman,

55
00:04:11,910 --> 00:04:18,839
because I find it easier than logging in here and chasing it around the terminal, then that is just

56
00:04:18,839 --> 00:04:19,529
my preference.

57
00:04:20,010 --> 00:04:23,670
And essentially what we want to change over here is the error.

58
00:04:24,570 --> 00:04:29,850
Now, of course, we're looking for custom error and then we want to go with status code.

59
00:04:30,120 --> 00:04:35,220
And then as far as the JSON, while the same thing, we're going to go with a message and we'll set

60
00:04:35,220 --> 00:04:36,850
it equal to our custom.

61
00:04:36,870 --> 00:04:40,200
Our that and of course, we're looking for the message.

62
00:04:40,680 --> 00:04:44,120
And you'll notice that at the moment there's really no difference.

63
00:04:44,130 --> 00:04:51,750
So if I go back and again, try to register, Susan, even though the email is already in use, I see

64
00:04:51,750 --> 00:04:52,170
this one.

65
00:04:52,170 --> 00:04:53,790
I see or hear a message.

66
00:04:54,060 --> 00:04:56,640
And this is going to be that Mongo's one.

67
00:04:56,880 --> 00:04:59,550
And of course, I still have this five hundred.

68
00:05:00,000 --> 00:05:02,640
So technically, nothing changed.

69
00:05:02,910 --> 00:05:09,750
Now, of course, this is where we'll work on right now, we're after our custom API instance will go

70
00:05:09,750 --> 00:05:09,990
here.

71
00:05:09,990 --> 00:05:15,040
If and essentially if you want to check, we will have to take a look at the object one more time.

72
00:05:15,300 --> 00:05:20,760
So this is why I'll keep both of these responses, because it is hard to find that area over here.

73
00:05:21,090 --> 00:05:25,380
And essentially, I'll be sending back the object just so you can see what is happening.

74
00:05:25,680 --> 00:05:27,450
And I think for the time being, I'll get the.

75
00:05:27,690 --> 00:05:34,440
So let me just say right here, let's send one more time and what you'll notice that, of course, if

76
00:05:34,440 --> 00:05:38,190
we have that duplicate issue, this is the error that we're getting back.

77
00:05:38,640 --> 00:05:46,830
And effectively what we want to do, we want to check if the error code exists and the code is equal

78
00:05:46,830 --> 00:05:53,250
to this eleven and then three zeros, then we want to send back this value, this Imman one.

79
00:05:53,370 --> 00:05:58,270
So then we want to access this key value and then just come up with some kind of message.

80
00:05:58,620 --> 00:06:01,920
So it's going to look something like this where we're going to go with F.

81
00:06:02,850 --> 00:06:08,960
Then, like I said, we're checking for error code and keep in mind that I'm not talking about custom

82
00:06:08,970 --> 00:06:15,150
error here, I'm saying, yeah, there is a error object and I want to check whether it has the code

83
00:06:15,150 --> 00:06:15,630
properly.

84
00:06:15,900 --> 00:06:23,760
And if it has done, I want to check more specifically if the value is equal to 11 and three zeros.

85
00:06:24,150 --> 00:06:26,340
So go here like this and that.

86
00:06:26,370 --> 00:06:32,370
If that is the case, then I know that I can access, of course, this key value property and I just

87
00:06:32,370 --> 00:06:33,720
need to come up with some kind of message.

88
00:06:34,080 --> 00:06:38,630
Now, what we'll do over here, like I said, will modify these values over here.

89
00:06:39,000 --> 00:06:40,730
So in here, we just set up the defaults.

90
00:06:40,770 --> 00:06:47,100
If the error is not going to match any of our conditions, that either will use the actual error status

91
00:06:47,100 --> 00:06:50,060
code if it exists as well as the message.

92
00:06:50,070 --> 00:06:52,790
If not, then of course, we'll have these defaults.

93
00:06:52,800 --> 00:06:53,790
Hopefully that is clear.

94
00:06:54,240 --> 00:06:58,080
And let's just start by setting up the message now how we can do that.

95
00:06:58,080 --> 00:06:59,580
Well, we can go with custom error.

96
00:06:59,820 --> 00:07:01,350
So that's our object.

97
00:07:01,500 --> 00:07:03,750
And essentially we're overriding the message.

98
00:07:03,840 --> 00:07:04,250
Correct?

99
00:07:04,590 --> 00:07:07,410
I'm just saying the message value will be equal.

100
00:07:07,560 --> 00:07:15,420
And in this case, I'm going to use the template Michal's because I'll pass in the actual error value.

101
00:07:15,690 --> 00:07:21,690
And as far as text, I'm just going to say duplicate value entered for now.

102
00:07:21,690 --> 00:07:23,840
Texas, the value would say error.

103
00:07:24,060 --> 00:07:28,550
And again, remember, we're looking for this property over here, correct.

104
00:07:28,860 --> 00:07:32,940
So say here error and we're looking for key value.

105
00:07:33,330 --> 00:07:35,910
And then we just want to continue with the text field.

106
00:07:36,270 --> 00:07:40,050
Please choose another value.

107
00:07:41,030 --> 00:07:46,810
Another value, and of course, I also want to change the status code, so in this case I'll go with

108
00:07:46,820 --> 00:07:53,900
custom error and I'm looking for status could probably and I'll set it equal to 400 because this is

109
00:07:53,900 --> 00:07:54,830
a bad request.

110
00:07:55,250 --> 00:08:01,250
And since we can clearly see the error object, since we already have all of the code that we need,

111
00:08:01,580 --> 00:08:07,520
again, I'm just going to hop around here and I'll come on this one out because I will use this one

112
00:08:07,520 --> 00:08:10,820
in a later videos and then we'll go back to this custom one.

113
00:08:11,060 --> 00:08:14,330
And once we set everything up now, of course, we can navigate here.

114
00:08:14,580 --> 00:08:16,900
We can send and of course, this is the response.

115
00:08:17,210 --> 00:08:18,410
So now we have the message.

116
00:08:18,410 --> 00:08:23,840
We have duplicate value entered for object object and work on this one a little bit later.

117
00:08:24,200 --> 00:08:26,930
And what's really cool that of course, now we have 400.

118
00:08:27,230 --> 00:08:30,140
So we can clearly see that it is a better question.

119
00:08:30,650 --> 00:08:36,169
Now, if we don't want to send this object object, which of course is not that helpful, we'll simply

120
00:08:36,169 --> 00:08:40,600
need to use a little bit of JavaScript where, of course, we have the object.

121
00:08:40,610 --> 00:08:42,980
So our key value is an object.

122
00:08:43,270 --> 00:08:46,130
And JavaScript we have object dot.

123
00:08:46,130 --> 00:08:48,680
And of course, in our case we're looking for keys.

124
00:08:49,070 --> 00:08:55,160
So this will give us a array of keys and of course in this case will only have the email.

125
00:08:55,400 --> 00:09:01,840
So it's going to look something like this, whatever object is, and then we'll pass in our error and

126
00:09:01,850 --> 00:09:06,340
key value and as a result will get back array of the keys.

127
00:09:06,350 --> 00:09:07,880
So let's send one more time.

128
00:09:08,200 --> 00:09:09,380
Let's go back over here.

129
00:09:09,560 --> 00:09:13,010
And now, of course, we'll have duplicate value for the email field.

130
00:09:13,250 --> 00:09:16,630
Please choose another value and put this in place.

131
00:09:16,650 --> 00:09:24,380
Now we're handling the duplicate error and essentially we're sending back more friendly user response.

