In the last couple of videos we've spoken a little bit about area handling and now we're at the point
where I think that we've got two distinct driving forces in doing all this area handling stuff So issue
number one with air handling.
We need to make sure that we always have a very consistently structured response from all of our different
services we put together and no matter what goes wrong in any of those services whether it is some invalid
data into a request handler or an error in database access or some other error we need to make sure
that we somehow capture all those different things successfully and structure the air response in some
very consistent fashion that is driving force number one and number two.
Let's talk about how we are going to solve each of these issues.
All right.
So a little solution here so to make sure that we always capture all errors and process them and structure
them in some identical consistent fashion.
We're going to write an error handling middleware we're then going to wire that up to express and anytime
that something goes wrong instead of application Express is going to capture some kind of error and
send it to our area handling middleware.
So we're going to do some processing inside of this area handling middleware.
The other big thing we need to make sure that no matter what goes wrong no matter where some error occurs
we need to somehow capture it and send it off to express so that it can be forwarded on to our area
handling middleware so we're going to make sure that we capture all possible errors inside of all of
our different requests handlers and anytime something goes wrong we're going to pass the error off to
express and effectively that really just means using the next function inside of our request handlers
that might not make a lot of sense right now.
So let me give you a quick reminder on how EXPRESS Does your handling in general I'm taking a look at
the Express area handling documentation right here.
So there are several sections inside of here.
Overall it's a two step process for dealing with errors inside of Express.
First off if anything ever goes wrong inside of a synchronous root handler and that's what you see right
here.
It is a synchronous right handler because there is no async code inside of it.
No callbacks no promises no async await if anything ever throws an error that Express is going to automatically
capture that error and forward it on to any error handling middleware that you and I have authored we
can then do some processing on the air and automatically send a response back.
The second case in which we'll get an error is when we do have an async request handler.
So this is an example of an async request handler because it has an async callback inside of here.
This code is going to try to read a file off the hard drive and then at some point in time the future
that callback function gets invoked.
This is also appropriate or relevant if you're making use of promises or the async await syntax so if
you have an async root handler and something goes wrong you and I need to manually capture that error
either by receiving it as an argument into a callback function or setting up a try catch block.
We're then going to take that error and pass it off to the next function that is provided automatically
into our root handler.
So again we've got scenario number one but they synchronous root handler Express is going to take care
of that automatically for us And scenario number two where we have an async root handler and that's
going to be any root handler that makes use of a callback function promise or async await syntax so
that is how we're going to somehow capture in air and pass it off to the next function to actually build
a error handling middleware.
Let me give you a quick reminder on that as well.
On the same documentation page I'm going to scroll down a little bit.
And there are some directions on here for writing an error handler.
So we defined a middleware function and all we have to do is make sure that the middleware function
is going to receive four different arguments whenever we pass a middleware into Express.
It's going to automatically pass that function and count the number of arguments so express is essentially
aware of the number of arguments that are going into a middleware if we have a middleware function with
four arguments Express is going to assume that this is a function meant to handle errors that occur
inside of one of our different root handlers.
So you and I are going to write out a function that looks very similar to this right here.
We're gonna make sure that any time it gets called we somehow inspect that error figure out what goes
wrong and then automatically send a response back now in all the stuff you're going to start to see
that there are a couple of interesting corner cases.
So right now I'm presenting you a very simple and straightforward way of dealing with this air stuff.
Well we are going to publish this stuff up beyond what you see in this documentation to a pretty good
degree.
We're going to eventually have a pretty sweet little airy handling process now that we understand the
two general things that we're going to do here.
Number one we're going to make sure we capture all possible layers.
Number two we're also going to write an error handling middleware.
Let's take a pause right here and start to implement both of these things in the next video.