We spent all this time on air handling.
So now surely there is no way that we could break everything by just adding in one keyword.
Right.
There's no way that could possibly happen.
Oh whoops.
Yeah.
Turns out we can break all the error handling stuff we just did by adding in one very small keyword.
So in this video we can take a look at what that keyword is.
We're gonna understand what it does to us and how we're gonna solve it and make it work with our area
handling approach.
Gonna find that little apt on all statement we just added in a moment ago and I'm gonna put it on the
async keyword like so now surely that's not going to do anything to us right.
We don't actually have any await syntax inside of your.
All we're doing is throwing in an error.
Well let's save this.
Go back over to post man and make that same request once again Hey wait a minute it's stuck in sending
request.
What's going on here.
How did we just break this by adding on that one little keyword Well you've got to understand what async
does behind the scenes.
Never you mark a function as async that function is no longer going to immediately return any value.
Instead it's going to return a promise that's going to resolve with some value in the future even if
we throw in error inside there.
How does this relate to express.
Well let's go back to the Express error handling documentation once again.
You might recall that I had mentioned very briefly that if we had a synchronous root handler and we
threw an Air Express will automatically capture that thing and throw it off to any error handling middleware
we have to find inside of our application.
However if we have an asynchronous middleware or an asynchronous root handler and when we say asynchronous
we are talking about any root handler that's going to have a secured applied to it a callback or a promise.
Then we have to rely upon this next function instead and that's what we just did.
We now have an asynchronous root handler.
So if you want this thing to work correctly we have to receive that next function.
And when we decide to throw in error we don't actually throw it.
Instead we will call next with our air let's now save this look back over to man and try this again.
Looks like it's working once again.
Well obviously rather than doing the throw new air versus next I would rather not have to worry about
this next thing at all.
It's something it's very particular to express and it really requires engineers to understand what next
is doing behind the scenes I think would be a lot better if we could somehow figure out a way to stick
with the throw keyword instead and just make this work.
Well to do so we're going to install a little package a little package that's going to change the default
behavior of how express handles root handlers like the one we just put together right here.
This little package is going to make sure that express waits or essentially does in a weight statement
on this function.
And so if we have some async code inside of here for example using async await express we'll start to
watch for any errors that we throw at any point time inside this function but essentially it's just
augmenting the default behavior of Express and kind of bringing it into the present age of using async
await.
This package is extremely easy to use.
Let's just go take a look at the documentation for it.
Very quickly I'm going to open up NPM the NPM J.S. dot com and at the top I'm going to do a search.
We're going to look for I do not recall the name of it off the top I had a gotta check my notes really
quickly.
There we go.
Turns out there's a dozen packages with very similar name which is why I didn't really just want to
randomly search for it so express async errors.
That's what we're looking for.
So here's express async errors you can read the documentation here if you want to.
It pretty much just does what I mentioned.
It's going to make sure that if we ever throw an error at some point time inside of an async function
this whole package is going to make sure that express listens for it.
Use this thing.
All we have to do is install it and then require it in or in our case import it in right after Express
that's it.
That's all we have to do so let's go over to our terminal and install it into our project
inside of our ticketing directory.
I'll change into off and do an npm install express async errors.
This is a very small module so it should install rather quickly after installing it all then go backwards
my index start to yes file a go up to the top I'll find where we import express and then right after
that I'll add an import express async errors and that should be at I'll save this.
You'll notice that I still have code that was causing us trouble a moment ago.
But if I go backward a postman and send a request once again.
Now it's working and even though we've got that async keyword in there.
So this is going to fix up not only the root handlers we define inside this file but inside of other
files as well.
So for example if we go back over to sign up here's the sign up file find the actual root handling function
and put the async keyword on there as well.
Now we can go backward a postman I'll fix up my URL so going to delete all that gibberish at the end.
I'll make sure I'm still making a post request.
Now if I send this off.
Yeah this works as well.
Well I'd say this looks pretty good.
Now we can handle errors across the board and I'm pretty sure that really actually is the last error
related topic we need to worry about for a long time.
So with that in mind because right here in a start taking a look at actual authentication is what we
actually originally set out to do in just a moment.