‫Welcome back, now before we go deep
‫into actual error handling, let's first write
‫a handler for undefined routes.
‫So basically for routes that we didn't assign
‫any handler yet.
‫And first up, we need to start our application here
‫which we didn't quit after the recent debugging session.
‫So MPM start.
‫And so let's wait for a database connection.
‫Here it is, and so let's now go to Postman here
‫and try to hit the route that we didn't yet define.
‫So I'm just gonna go ahead, copy this URL here,
‫all right, and so for example
‫let's say that instead of apiV1
‫tours,
‫we would say just api/tours.
‫Okay so in that case we would get this HTML result,
‫all right, so Express automatically sends
‫this HTML code here, along with a 404 Not Found
‫error code in case that there is not any handler
‫for the route that was requested, okay.
‫Or, we could also simply misspell tour here for example.
‫So for example in this case we would
‫still get the same error.
‫Now there's also another situation which is if here
‫after tours we specify something else,
‫let's say just something like this.
‫And so let's take a look at the error that we get,
‫and so now we get that the cast to object ID failed.
‫And that's because we actually have a route that
‫accepts an ID parameter here after the tour/, right.
‫And so MongoDB is basically trying to find a document
‫with this ID, but cannot convert it to a valid MongoDB
‫object ID, all right.
‫So again that is a different situation, let's just
‫keep working with this one, so again, where we get
‫this error but in this HTML form.
‫Now since we're doing an API here, it doesn't make
‫much sense to send back HTML, right, and so let's
‫now fix this and basically create a handle function
‫for all the routes that are not cached by our routers.
‫Okay, so let's go back to our application here
‫and open up app.js.
‫Okay, so that's basically the definition
‫of our Express application.
‫Now before doing anything else
‫let's actually get rid of this middleware here
‫that we do no longer need.
‫So we just used this here in order to demonstrate
‫the concept of middleware, and so at this point
‫we no longer need that.
‫All right now how are we gonna implement a route
‫handler for a route that was not cached by any
‫of our other route handlers?
‫So for doing that remember that all these middleware
‫functions are executed in the order they are in the code.
‫And so the idea is that if we have a request
‫that makes it into this point here of our code
‫then it means that neither the tourRouter
‫nor the userRouter were able to cache it, okay.
‫And so if we add a middleware here after these routers
‫it will only be reached again if not handled by any
‫of our other routers, okay.
‫So let's do that, and then really understand how it works
‫after it's already implemented.
‫So we're gonna implement a route handler,
‫and so we say app.
‫and now the HTTP method for which
‫we want to specify the route.
‫Now we could use get here right, so just like we did before
‫but then what about post requests, or delete,
‫or patch requests?
‫You would then have to write handlers for these as well,
‫and we don't want that, we simply want to handle
‫all the routes, so all the URL's, for all the verbs
‫right here in this one handler, okay.
‫And so in Express, we can use app.all.
‫And so that's then going to run for all the verbs,
‫So all the HTTP method, all right.
‫Next up we specify the URL, and since here
‫we want to handle all the URL's that were not handled
‫before we can use the star here, which is going
‫to stand for everything, all right, and then the
‫rest is just a regular middleware function,
‫just like before.
‫So request,
‫response, and next.
‫Okay, and what do we want to do here?
‫Well we simply want to send back a response
‫in the JSON format, so not the HTML that we have right now.
‫So res.status,
‫and here let's set a 404, so Not Found
‫and then a JSON response, so just like the usual one
‫where we set the status to fail.
‫So just a regular adjacent formatted response.
‫And then some kind of message here, and actually
‫let's do a template string here,
‫because I want to put a variable in there.
‫So can't,
‫find.
‫And then we can use req.originalUrl
‫okay, so that's a property that we have on the request
‫which is as the name says, the URL that was requested,
‫all right.
‫So this new response that we're going to send back now
‫is a lot better than the HTML that
‫we were receiving previously, so let's now test that out.
‫And indeed, now we get a JSON error message back here.
‫And so here we also get the URL that was requested,
‫and indeed it's the one that we tried to access,
‫but it is not available, all right, great.
‫Now again, why did this work?
‫So again the idea is that if we are able to reach
‫this point here then it means that the request
‫response cycle was not yet finished
‫at this point in our code, right.
‫Because remember that middleware is added
‫to the middleware stack in the order that it's
‫defined here in our code.
‫And so basically this code here runs first,
‫and so if the route was matched here in our tourRouter
‫then our request would never even reach this code,
‫and so then this would not get executed.
‫And so this should basically be the last part
‫after all our other routes, all right.
‫And if I were, just to try this out,
‫now put this right at the top of our application
‫then you will see that no matter what request
‫we're gonna do, we will always get this same response.
‫Right, so let's test that,
‫and indeed now we get JS error message,
‫and so that's because all request
‫now reach this route handler here, and it's actually matched
‫because it's a GET request, which is part of all the verbs,
‫right, and then all of the routes, so all of the URL's
‫are cached here, and so of course it handles
‫that URL that we just did here.
‫And the same of course, for example for a delete tour.
‫So the same thing would happen we would always
‫get the same response,
‫all right.
‫So let's of course put it back, but this was
‫just to demonstrate how and why this works,
‫all right.
‫Great so this an important part to make our API
‫a bit more user friendly, but next up let's now
‫start learning about real error handling.