All right, so how we could set up the logic in our controller where instead of the generic 501?
We're going to be getting 400.
Well, let's think about it.
We're getting this error class or constructor from where we're getting it from JavaScript.
Correct.
So already provided for us and we know that in JavaScript, we could extend from a certain class.
We're essentially.
I can just add some extra logic and then use that instance instead.
So let's try it out and they quicksand out.
If you need a refresher on Joske of class syntax as well as how to implement subclass thing with extends,
as always, Amdocs is a great place to start.
And we're going to do that here, right in our controller.
And then once we have all the logic in place, then we'll separate the concerns.
So for now, let's do this way.
Let's create a class, and I'm going to call this one custom API error.
Now, name is really up to you.
When I said, we're going to go with extends and our mike show.
So once we extend, then we want to go with constructor and nanoporous here.
The message and not in the curlies will go with super value message.
And I want to add that status code.
I simply want to add the property on my instance.
Now, what property do I want to add?
Well, this one.
Because at the moment, this one is still the generic 501.
And the way we do that, we go with this, that status and code, and that is equal to my status codes,
correct?
Again, you cannot call this as 400 markers, so I'm going to go status codes and bad request.
So that's the 401.
And now, instead of going with error, I'm going to go with custom API error so I can still pass in
the message just like in the normal error or in this case, there is this property status code on it
and then we pass here ready for it.
So now what do we need to do is to go back to the error handler and in the default error, do the same
thing.
Like with message where I'm going to say if the error object that is coming in has the status quo,
probably use that one and again in here, remember that we're overriding that anyway.
So let's just go here and let's just say error.
That status code, if it is present, use that one.
If not, use the jack one.
And now let's try it out.
And of course, my where I'm going to go back, I'm going to send it and check it out.
Not only I have please provide all the values, I also have the 401.
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
email and you'll see that the result is exactly the same.
Apart from the fact that not particularly bright line, I have a syntax error.
And there you go.
Now I have please provide all values.
Beautiful.
So that means that my logic works where in here we are extending from the error class and we invoke
the instance.
So we invoke the custom API error.
And there we are passing the message and the status code is already hardcoded here.
So essentially it is set to 400.
So every time I'll use the custom API or instance, what do you think is going to happen?
Well, we'll get that 400 as a status code and then we can always provide whatever message we want,
just like with normal error class.