In this video.
Let's go ahead and refactor our error handling inside our root.
You can see that any time you want to throw an error unless you go through this process.
So let's find a way where we can centralize this one.
So let's go ahead and create a folder for this.
So I'm going to create a folder called Utils.
And inside I'm going to create a file called App Error.
I make use of uppercase because we are going to create this one using a class.
So in case you are not familiar with classes in JavaScript, don't worry, I will create a different
function where we are going to use normal function syntax.
So first let's use normal function syntax here and then after that I'm going to show you how we can
make use of classes in JavaScript to achieve the same concept.
So here we go.
Before we start the implementation, let's look at the arguments or the properties the function needs.
First of all, it needs the status code, the message, and this are the most important thing here.
For starters, we always know it is failed so we can choose not to provide and we can give it as a default
value for that.
So let's send this to arguments starters and then the message.
All right, So here we go.
So make use of costs and then the app error is equal to error function.
And for this function, I'm going to take in the message of the error and then the status code that
we want to provide.
So inside here we are going to create instance of the error from the error class, which is equal to
new and then the error.
And for this we are going to pass in the message because for this error, the only argument it takes
is a message we pass into this class.
So now here next is let's add the status code.
So the error dot status code, which is equal to the status code that we pass in.
Lastly, we need to return the value for this function.
So going to be the error and let's export this module.
Dart's export is equal to our error.
Now inside the server file, any way I want to throw an error, I'm going to call that function.
So let's require the error function here and now we are going to remove all this code from here and
are going to call the app error.
Then you pass in our message, which is network.
Error.
And then the second argument is going to be the status code, which is 500.
So what comes back from this one is the actual error.
So cost the error here and next is going to pass in to the next maneuver, which is down here.
So now let's make the request and let's see.
We can see that we have network error.
And then with the status code 500, that is internal server error and we can call the same function
here in any root.
For example, this log in here.
So now I'm going to call the function here and here the message is going to be as you are not logged
in and 401 meaning not authorized.
Now let's go to the file and let's hit send.
And there we go.
We got a message.
But the problem is we are not getting the status code is always 500.
So let's see what is wrong.
Oh, sorry.
We need to send the error for this function call.
So now let's remove this one.
And next is let's pass in the error here.
So let's make the request.
And we got the status code for one with a message.
You are not logged in so well from this one.
Let me make sure I type in the correct.
Yeah.
Okay.
So again, let me show you one more time.
And indeed, we have it.
So this is one way where we can create a function to create an error.
But if we want to use class based one, let me show you this one or so.
So let me comment this one.
So here we go.
Make use of class and then uppercase up error because we are going to create an instance of this error
and we need to extend from the error class as that.
And next is I don't.
And next is our brain and mind function called constructor.
And it's going to be called when an object is created from this class and I'll pass in the message of
the error and then the status code also.
Next is we will call the super.
Then you pass in the message because for this it takes in only one agreement.
That is a message.
So let's go ahead and add the status code to it, which is equal to the status code.
So let's go ahead and export this one.
We can remove this one and make use of app error here.
So next is inside any route I want to call, I'm going to make use of that.
So let me change this one to uppercase app error.
So next is let's modify this function or let's change this one to uppercase A, And because it's a class,
we're going to make use of new to create an instance of this class.
So now that is it.
So let's go ahead and make the requests and now let's check it out.
You can see that network error and is working fine.
The same thing applies to the profile.
Make use of this uppercase up error and then name equal to that.
And let's send the requests and we have the network error and also we call something stock trace stock
trace is a code on a file where that particular error occurs.
So for this one, we can see that it is giving us the message.
But if you want to know what file does this error okay, you're going to use what is called the stock
trace.
So inside the error handler here where we are catching the error, we can also throw the stock trace
also.
So here are going to be the stock.
And we have access to the error dot stock.
Now save it and let's make the request.
And now we can see that this time around I get beautiful message with the stock, meaning the file that
this error occurred and what line.
So if you look through, have a look can say that this is the file and then if you look through it,
you're going to see the particular line that this particular error.
Okay.
So it's somewhere unless you take your time and then look through.
Well, you can see that it starts from here and then it ends somewhere here.
That is my server dot just filed on line number ten.
So if I check my server file, line number ten, let's see.
And this is where the error.
Okay, so start trace, help us to debug our code where a particular error.
Okay.
So the next step is how can we handle asynchronous errors, for example, when we are dealing with database?
So let me show you how we are going to do this one in the next video.