All right.
And up next, let's discuss async errors.
How are they different from synchronous errors and what are going to be our approaches to handle them?
So first, let's navigate to job controller and let's establish that job dot create.
Effectively is asynchronous.
So what happens if there's some kind of error?
So at the moment, yeah, everything is nice.
We pass in the req.body and we're good to go.
But what happens, for example, if I pass here the string, the create is looking for object.
I can tell you that right away.
But what if I go with something?
Let's see the result we get.
So let me navigate to Thunder Client Create job doesn't really matter what you send over here.
The main point is following notice we'll get this massive error in the console.
Okay.
But then our entire application crashed.
So unlike the previous setup for synchronous errors, yes, we log still the error, but we nicely send
back the response and also our server is still running.
So yeah, we handle the error, but then we keep accepting the requests.
It's not the case right now.
So pretty much every time we'll have some kind of error happening.
Well, our whole server is going to go down and of course we want to fix that.
That's not the application we want.
And essentially we have two approaches.
We can set up, try and catch manually, which we're going to do in a second.
Also, we can install package so that way we don't have to do it pretty much for every controller.
So let's try the manual approach first.
So I'm going to go here with try and catch.
Then let me move these two lines of code up.
And then as far as the catch one, let's just send something generic.
I'm going to go with status 500.
That.
And we're going to go with Jason.
And let's go with message and server.
Error.
Let's save it.
And now let's try it out one more time.
So let me send and check it out.
We get back the 500 response.
Message server error.
And since we are working with Morgan, we can also see in the console that we made a post request and
the response is 500.
But the main point here is that our server is still running.
So I can go to, for example, get all jobs and yes, of course I'll get those hardcoded jobs, but
everything still works.
This is still 200.
Now if we want to log the error again, we'll just do it manually for now.
We'll say over here, log an error and we're pretty much good to go again.
I'll create the job.
Yes, I'll get 500.
Yes, I'll get the log in the console.
But again, the main point is our server is running.
Okay.
Hopefully that is clear.
So essentially since we're performing asynchronous operation, we want to wrap it in, try and catch.
But your next question probably is following.
Okay.
Does that mean that pretty much we'll have to repeat this logic for every controller?
And the answer is, well, yes and no.
Technically, you can do it this way.
So essentially you set up the try and catch for every controller, which is a little bit annoying,
especially if you have, I don't know, 30 controllers or we can install a package that effectively
will do that for us.
And essentially this is what I'm going to do right now.
So let's navigate to our read me and let me show you that there is a package by the name of express
async errors, which essentially just provides that error to our error middleware in the server or here
because like you just saw, it's not happening by default.
Now, one last thing that I want to mention.
Make sure that the import is at the very, very, very top on the server.
Some students have complained that functionality doesn't work.
If that's not the case.
So in order to install this package, you want to go to NPM.
I then express async errors and this is the version that I'm using.
And then once you install the package, you want to go with import.
And notice in this case we have the syntax.
So import and then pretty much the package name again, please place it at the very top and you'll avoid
some unnecessary bugs.
So in here, let's go with import then we're going to go with express async errors.
Just to stay on the safe side, I'll actually stop my server manually clear everything npm run dev and
then let's go to create job in the job controller and let's remove those try catches and let's see what
happens because again, we still have the error.
This should be an object and I'm actually passing here the string.
So let me create the job one more time.
I send and notice my server is still working.
I'm getting this 500 as far as the response and I also nicely can see my log over here.
Why?
Well, because in my error middleware we're logging it right now.
So essentially the express async errors package.
Handles any asynchronous errors and passes them to our middleware.
Since it's not happening by default and since I don't want to type.
Try and catch for every controller we're about to create.
Well, that's why we'll use this awesome package.
Just make sure.
To place the import at the very, very top in the server.js file.