1
00:00:00,300 --> 00:00:07,530
All right, so how we could set up the logic in our controller where instead of the generic 501?

2
00:00:07,860 --> 00:00:09,690
We're going to be getting 400.

3
00:00:10,170 --> 00:00:11,760
Well, let's think about it.

4
00:00:12,300 --> 00:00:17,490
We're getting this error class or constructor from where we're getting it from JavaScript.

5
00:00:17,790 --> 00:00:18,120
Correct.

6
00:00:18,450 --> 00:00:25,950
So already provided for us and we know that in JavaScript, we could extend from a certain class.

7
00:00:26,920 --> 00:00:27,790
We're essentially.

8
00:00:29,070 --> 00:00:34,350
I can just add some extra logic and then use that instance instead.

9
00:00:35,040 --> 00:00:37,320
So let's try it out and they quicksand out.

10
00:00:37,320 --> 00:00:44,520
If you need a refresher on Joske of class syntax as well as how to implement subclass thing with extends,

11
00:00:44,880 --> 00:00:47,700
as always, Amdocs is a great place to start.

12
00:00:48,180 --> 00:00:51,600
And we're going to do that here, right in our controller.

13
00:00:51,990 --> 00:00:57,150
And then once we have all the logic in place, then we'll separate the concerns.

14
00:00:57,600 --> 00:00:58,980
So for now, let's do this way.

15
00:00:59,220 --> 00:01:03,870
Let's create a class, and I'm going to call this one custom API error.

16
00:01:04,140 --> 00:01:06,480
Now, name is really up to you.

17
00:01:06,860 --> 00:01:11,220
When I said, we're going to go with extends and our mike show.

18
00:01:11,580 --> 00:01:17,370
So once we extend, then we want to go with constructor and nanoporous here.

19
00:01:17,750 --> 00:01:23,280
The message and not in the curlies will go with super value message.

20
00:01:23,730 --> 00:01:26,130
And I want to add that status code.

21
00:01:26,400 --> 00:01:30,020
I simply want to add the property on my instance.

22
00:01:30,030 --> 00:01:31,660
Now, what property do I want to add?

23
00:01:32,250 --> 00:01:32,940
Well, this one.

24
00:01:33,450 --> 00:01:36,960
Because at the moment, this one is still the generic 501.

25
00:01:37,440 --> 00:01:46,350
And the way we do that, we go with this, that status and code, and that is equal to my status codes,

26
00:01:46,350 --> 00:01:46,770
correct?

27
00:01:47,010 --> 00:01:53,580
Again, you cannot call this as 400 markers, so I'm going to go status codes and bad request.

28
00:01:53,850 --> 00:01:55,350
So that's the 401.

29
00:01:55,770 --> 00:02:04,290
And now, instead of going with error, I'm going to go with custom API error so I can still pass in

30
00:02:04,290 --> 00:02:11,400
the message just like in the normal error or in this case, there is this property status code on it

31
00:02:11,850 --> 00:02:13,920
and then we pass here ready for it.

32
00:02:14,100 --> 00:02:20,850
So now what do we need to do is to go back to the error handler and in the default error, do the same

33
00:02:20,850 --> 00:02:21,270
thing.

34
00:02:21,270 --> 00:02:27,530
Like with message where I'm going to say if the error object that is coming in has the status quo,

35
00:02:27,560 --> 00:02:33,480
probably use that one and again in here, remember that we're overriding that anyway.

36
00:02:33,900 --> 00:02:36,960
So let's just go here and let's just say error.

37
00:02:37,230 --> 00:02:42,630
That status code, if it is present, use that one.

38
00:02:42,990 --> 00:02:45,270
If not, use the jack one.

39
00:02:45,810 --> 00:02:46,830
And now let's try it out.

40
00:02:46,830 --> 00:02:50,940
And of course, my where I'm going to go back, I'm going to send it and check it out.

41
00:02:51,240 --> 00:02:56,100
Not only I have please provide all the values, I also have the 401.

42
00:02:56,610 --> 00:03:01,710
And just so you don't think that I'm messing with you, I'm going to add the name and let's remove the

43
00:03:01,710 --> 00:03:04,620
email and you'll see that the result is exactly the same.

44
00:03:04,920 --> 00:03:09,660
Apart from the fact that not particularly bright line, I have a syntax error.

45
00:03:09,930 --> 00:03:11,490
And there you go.

46
00:03:11,790 --> 00:03:14,880
Now I have please provide all values.

47
00:03:15,090 --> 00:03:15,600
Beautiful.

48
00:03:15,930 --> 00:03:25,560
So that means that my logic works where in here we are extending from the error class and we invoke

49
00:03:25,560 --> 00:03:26,370
the instance.

50
00:03:26,880 --> 00:03:30,420
So we invoke the custom API error.

51
00:03:30,930 --> 00:03:36,120
And there we are passing the message and the status code is already hardcoded here.

52
00:03:36,840 --> 00:03:39,390
So essentially it is set to 400.

53
00:03:39,750 --> 00:03:46,530
So every time I'll use the custom API or instance, what do you think is going to happen?

54
00:03:46,890 --> 00:03:53,550
Well, we'll get that 400 as a status code and then we can always provide whatever message we want,

55
00:03:53,550 --> 00:03:56,130
just like with normal error class.

