Now that we've wrapped up all this air stuff I figure why not do some more air handling stuff.
Yeah.
Sorry I am serious.
I want to go very quickly through the process of creating a new type of area in our application is going
to be really simple and straightforward and you're going to see how easy it is to add into new area
handling stuff.
So I want to try to add in a new air to handle the case in which a user goes through a route that does
not exist.
So here's what we're going to do.
We're gonna first again by finding our ears directory inside there.
I'll make a new file called not bound Air dot t yes so this will be an area that we want to yield to
throw at some point in time.
Anytime a user tries to navigate to some route that doesn't exist any time we want to make a new error
at the very top we will import base air or see me.
Customer
rom custom air will then export a new class named after the air we're trying to make.
So not found air.
This is going to extend custom air inside of here.
We need to define a status code to send back to the user in the response will say status code for 0
4.
And by the way if we didn't add that in typescript will tell us we need to do it.
You'll notice that we're getting air right now saying hey you need to implement serialize errors and
status code we could define that status code we do need to still define our constructor because we need
to not only provide some default message your message.
Something like let's say root not found and we do also have to run that little object set proto type
of thing so object set prototype of this and not found air not roto type.
And then finally we'll implement serialize errors.
And the only requirement for this function we have to return an array of objects where each object has
a message that's a string and possibly a field so in this case let's just return an array with one hardcoded
object.
We'll give it a message of not bound and that should be it.
So now anytime that we throw this error in other words if we're in some root handler and we throw not
found or new not found error and we put in all like that then our Middleware is going to automatically
capture this error and return the appropriate response to the user so let's actually implement a root
handler that's going to capture a request to any arbitrary route that we have not actually defined back
inside of my index dot t s file at the very top.
After importing our air handler I will import not found air from errors not found there and then after
all of our different middleware as we wire up but before the error handler we'll put in an apt gets
to star like some
we don't even have to worry about a request or response in here because we're not going to access those
variables.
All we have to do is throw a new not found error like so that's it.
As soon as we throw this thing Express is going to capture the air and send it off to our middleware
not middleware it is going to take these status code it's going to call that serialize Eris function
and it's going to generate a response and automatically send response back to ever was trying to access
this route.
Let's save this and do a quick test back at post.
I'm going to try to go to a route that doesn't actually exist so I will try to go to API users sign
up slash whatever and we've only set this thing up for a get request.
So I am going to change this to get I'll then send it and lo and behold it works.
So we've got a status of four a four not found.
And we've got that same kind of exact air structure that we were looking for.
Awesome now as I mentioned this is only going to work with get requests right now.
We can make it work with any other arbitrary method if we just change apt I'd get right there to app
dot all I've done.
All is gonna watch for requests with any kind of method and with that any kind of routes.
Now we should be able to save that make a post request to the silly routes and we'll see the same thing.
Well I would say that this is not too bad.
Again that is how we add in some new custom air to our application read a file create a class.
Export it extend customer typescript is going to guide you on what you have to define it here.
So you have to define status code and serialize errors and then throw the air at some point time and
Express is going to take it from there.
So this looks fantastic.
Quick pause right here and I'll see you in just a minute.