In the last video we establish that we need to communicate more information from the request handler
to the air handler middleware through this air object but because we are using typescript we can not
just arbitrarily add in additional properties to the air object itself.
So in this video we're going to figure out how to solve this issue with a very typescript style approach.
So here's really the issue here here's what we want to do.
Summarized in single sentence we really want an object like an air.
We really want an air object essentially.
Well you want to add in some more custom properties to it.
That's we really want to do right now.
So any time that you say that you want something like X but you want to add in some more custom properties
that is usually a sign that you want to subclass something.
And that's exactly what you and I are going to do.
We are going to take the built in air object.
We are going to subclass it into two separate subclasses.
We're gonna call one request validation error and the second database connection air request validation
error is going to be a new type of air object that we're going to throw anytime that there is an error
with the incoming data on that incoming request then anytime that anything goes wrong with a database
connection we'll instead throw a database connection error.
So that would be like this right here.
The reason that we are making both these subclasses is that when we create the subclass we can add in
some additional custom properties to it that will further clarify the reason for the error.
Again we are trying to use these custom subclasses to communicate much more information from the request
handler down to that area handling middleware now at this point.
This might seem pretty crazy.
Let me show you a diagram to help you understand the entire big picture of what is going on here.
OK.
So kind of complicated diagram.
We'll go through it step by step they're going to first start off up here inside of our request handler.
You and I are going to create two new subclasses.
One is going to be called request validation error and another that is going to be called database connection
error because we are building these classes ourselves.
We can add in as many custom properties as we wish.
So for example on the request validation error subclass we can assign to it a new property called reasons
and that could be an array of objects that has exactly why the request validation failed then over on
this other subclass we can say that it needs to have a message property or maybe a reason property we
can call it whatever we want and it could be a simple string that says something like failed to connect
to database or request handler at some point time is going to throw either this or this to be precise
it's going to throw an instance of request validation error or an instance of database connection error
so that error either this one or this one is going to be thrown and it's eventually going to flow on
down to our error handling middleware then instead of our era handling middleware we're going to set
up some code inside there to inspect the area that it is receiving.
So we're going to check inside the error handling middleware and we're going to say OK I see an error
is coming.
Is it an instance of the request validation error class.
If it is then that means great.
We have an instance of request validation error.
Let's take that reason's property though this property right here and we're going to use that to build
a response to send back to the user if the incoming error is not a request validation error then we
will check to see if it is an instance of database connection error if it is then great let's take that
reason a property and we'll send it to the user and if we fail both these checks then we don't know
what kind of error this is it is a generic error.
And so in that scenario we should probably send some kind of generic error message back to the user.
So this is it this is the entire flow.
We're going to build out these subclasses to describe all the different things that can go wrong whenever
we throw one of these errors.
It's going to go into the area handling middleware.
We're going to try to figure out that type of error that it is and then we could build up some appropriate
response.
Now what I'm showing here on this diagram and specifically adding these properties right here is still
kind of a simplified version.
After we build an initial implementation of this we're going to come back and refine this even more
to add in way more automatic aspects to this.
I know in the last couple of videos I've said Hey just hold on please be patient.
We're almost at the point now where you're going to see that this really is an incredible error handling
approach.
So just a little bit more patients are going to start to write out this implementation in the next video
and you're really going to start to see all this stuff start to come together so quick pause and I'll
see you in just a minute.