So what I want to do now is create a custom error handler.
So whenever we have any errors, whether there's, you know, there's product we're trying to find that
doesn't match the ID or whatever, it always it always shows the same thing, an object with a message
and also the stack trace.
If we're in development and you can you can have your errors formatted absolutely however you want.
But I want to just keep it simple.
Now in Express, there's a default error handler and if you want to overwrite it, you can do that.
Let's see by just creating this a function called error handler and pass in error as the first argument
here.
And then you can do whatever you want as far as statuses and messages and all that.
Now, what we did with the async handler is actually an example right here.
So see this right here?
This this promise resolve dot then.
And basically this is what we did with the async handler.
So this will avoid the overhead of try catch blocks when using functions that return promises.
So that's why we added that async handler.
But what we're going to do is create let's just close this up for now.
Let's create in our back end middleware folder.
Let's create a file called Error.
Middleware dot.
JS And there's basically two functions that I want to create here.
The first one is going to be called NotFound, and basically this will this will be called if no other
middleware has handled the request and it will create a new error object and set the code to 404, which
is a not found error.
All right.
So basically, we're going to create a variable, set it to new error, and we're going to pass in a
message of not found dash and then the original URL, which we can get from the request object.
And then we set the status to 404 and we call the next piece of middleware passing in that error variable.
Now to overwrite the default express handler error handler, we're going to create a function called
error handler.
All right.
And then and that gets passed in not only requests response next, but also error at the beginning.
So I'm going to initialize a couple variables here.
So let's say status or status code, and I'm going to set that to a ternary.
So we're going to check to see if the status code is 200, because it may be if we're throwing an error,
we want to be able to throw an error from anywhere by doing this, say throw new error and then pass
in some message.
So we want to be able to do that.
And if it's 200, I want it to change to 500.
Okay.
If it's anything else like 404 or whatever, then that's what it's going to stay.
Okay.
So it'll be whatever that status code is.
So if it's 200, it'll be 500.
If it's something different, it'll stay whatever it is.
Then for the message, let's say let message equals and then error message.
So if I say throw new error and I pass in a message, we can access it from here.
And that's what we're setting to this variable.
Now I want to check for let's say, yeah, we can say that mongoose bad object ID or cast error.
And I showed you an example of that in the last video.
If we get a single product and we pass in an ID that doesn't exist, it gives us this HTML page with
a 500 status.
And if this is a cast error, so I want to check for this and show something different if that does
happen.
So we need to first of all, we're going to say if.
Yeah, we can do that.
So if.
Error.
I'll just type it out.
So if the error name is equal to cast.
Error, but I also want to check the kind so there's this kind value on the error.
Object.
And we want to check to see if its object ID.
If so, then we're going to set the message that we initialized up here to resource not found and we're
going to set that status code to 404.
Okay.
Then we want to just do our final response, which is going to be, let's say res dot status.
And we're going to pass in whatever that status code is.
And then the Json, which is just going to be basically an object with the message.
From the message variable, so we can just keep it like that.
And then I'm also going to send the stack trace, which can be helpful for developers if we're in development.
So if it's in production, it's just going to send this this pancakes and a stack of pancakes else.
Then it's going to send the actual stack to stack trace.
All right, then we just want to export these.
So we'll say export not found an error handler.
And then to use these, we have to go into our server.js and first of all, bring them in.
Let's say import not found an error handler from middleware slash error middleware.
JS And then we're going to go down here under all the routes and say app dot use not found and app dot
use error handler.
So now what I'm going to do is in our product route where we test to see if there's a product, we respond
with it else instead of doing this, which isn't even working because that cast error is is taking control.
Instead of doing that, I'm going to use our new error handler by first setting the status to 404 and
then throwing a new error and just saying product not found or, let's say resource.
Resource not found.
So now let's come back over here and let's get the product with one with the ID of one which doesn't
exist.
And now you can see we get a 404 response.
We get a message resource not found, and then we get the stack trace.
So no more HTML page, which doesn't make sense for a Json API.
Okay.
And if we want to throw any error, like let's go to our products route and just say throw.
New error and just say some error.
So now if I go to my products, get all products and I send we get a 500 because we didn't set it to
anything else.
Remember, if it's if it's 200, it just gets sets to 500 some error and then we get our stack trace.
Okay, so that little bit of code that we added makes things much more elegant when it comes to handling
errors.
We just do throw new error.
We can set a status right before it and we're all good.
All right.
So the last thing I want to do on the back end before we move back to the front end is create controllers.
Instead of putting all of our logic in the root file, we're going to have a controllers for products,
users and orders and put all of our functionality there.