In this video.
Let's see how we can handle errors gracefully inside our application.
And for error handling, we have a section where we talk about it in detail.
So let's see how we are going to implement that one inside our application.
All right.
So let's see how we are going to implement that.
So inside the metaverse, I'm going to create one file called Global Error handler for error handling
in experts.
This how we created count and going to be as global.
Is equal to function core as that.
And inside here, instead of two or three arguments, we are going to parse in for a given error request
response.
And next.
So any time experts see this kind of function, it health experts that this is error handling.
So inside here we are going to return the error that has been okay inside our application.
So let's have the properties that we want to send back to the user.
First one going to be the starters.
And for this we can send our custom message, for example, failed or something happened.
Or server error so we can send any type of these messages.
The next one is going to be the actual message.
And this one, who is the actual error that has occurred inside our application?
For example, if a user is trying to log in with invalid login credentials, we have access to that
on the message and I will show you as you move on the next one going to be the stack the stack represents
which file a particular error occurred and which line of code that error occurred.
So let's build up these properties.
So we're going to be as const stock is equal to and we have access to this properties on the error that
we threw.
So inside the controls, let me show you where we are throwing an error.
Let's get back to the users, which is this.
We can see that here.
If a user is being found, we are through in what is called status with this and then the message.
So we have access to this properties inside the global arrow handler.
But before we can have access to the arrows inside the global arrow handler here, unless we create
a specific function for it.
For the meantime, let's see how we can cut and send in the arrow.
All right.
So here on the arrow that came back, we have access to the stock as error dot stock.
The next one going to be the actual message, cast message.
And we have it on the error dot message and the starter is going to be starters is equal to the error.
Dart starters.
So in case a user provides starters, then you go ahead and use the error starters.
Otherwise we are going to use the default or failed and we can also send the status code.
So it's going to be status code is equal to the error.
If the user provide the status code upon throwing an error, we are going to use the error dot status
code.
Otherwise we use 500 million server error and let's send the response.
It's going to be as red dot status and we are going to use the status code and we are going to chain
with the JSON and we are going to send the message, then the stock and then the starters about the
message.
So here, let me rearrange.
This one status.
So let me bring the starters before the message.
The arrangement doesn't matter here.
Last step is we need to export module.
The exports is equal to global error handler.
So let's make use of that.
So let's get back to the set of file, which is this, and let's require the global error handler.
And like I said, we need to bring the error handler below all your route.
And this is a comment we have down here and make use of app don't use the pass in the global arrow handler.
This one anywhere in our code where we throw an error are going to have access to that and send back
as a response to the user.
So let's try to register a user with the same details and let's see what we're going to have.
And you can see that we have what is called user already exists.
And this is not an error because here we have the status quo, meaning everything is okay and we go
back this and this is an error.
So let's see how we can through rare error or some rare error whereby our global error handler will
catch it.
Let's get into that in the next video.