Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Using try/catch blocks to handler errors for each route can get messy after a while. We can write a middleware function that can abstract away the try/catch block to DRY up our code.
Snippet
function asyncHandler(cb){
return async (req, res, next)=>{
try {
await cb(req,res, next);
} catch(err){
next(err);
}
};
}
- If you'd like to know more about how this function, works, review the Reduce Error Handling Code When Using Async/Await video.
- npm module for handling async/await syntax in Express
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You might be noticing by now that
our code is getting a bit long.
0:00
All these try-catch blocks make things
verbose and kind of messy-looking.
0:03
We can use a piece of middleware to
prevent having to use try-catch blocks for
0:07
every single route.
0:12
Into app JS, I've added a middleware
function called asynchandler.
0:13
If you're following along with me,
see the teacher's notes to copy and
0:18
paste this function.
0:21
You don't need to worry about
the exact details of how this works.
0:22
Remember, that's kind of the point of
using a helper function or a library.
0:25
It abstracts complexities away for you.
0:29
But basically, it takes in a function,
wraps it in a try-catch block,
0:31
and passes any errors to the global error
handler we wrote in the last video.
0:37
We can use this middleware function
instead of using try catch,
0:42
in every single one of our code blocks.
0:46
So I'm going to scroll down
to our post route to /quotes.
0:49
And what we can do is,
parse our route handling callback,
0:54
this function here,
into our asyncHandler function.
0:59
I'll show you how that works.
1:03
First I'm going to comment
out the post route.
1:04
And I'll start a new one just below that.
1:08
Now the asyncHandler is going
to be our callback function.
1:14
When a post request is made to the home
route will run the async handler function.
1:18
Inside the asyncHandler function
we'll parse an anonymous function.
1:25
We'll need to parse this anonymous
function the request in response objects.
1:33
Inside the anonymous function we can put
all our code that handles the request and
1:41
creates a new record.
1:46
We'll go up here, uncomment this, And
we will copy and paste this in here.
1:51
This is exactly the code we had before,
1:58
only asyncHandler is acting
as the middleman here.
2:00
We're going to pass at the function
containing the function we want to run
2:03
when the request is made.
2:07
And it's going to call that function
inside of the try-catch blocks and
2:08
catch errors for us.
2:13
That way we don't have to have
a try-catch block in every single route.
2:14
One more thing, because we want to await
the creation of a record inside of this
2:18
anonymous function, the anonymous
function needs to be asynchronous.
2:23
Let's do this one more time.
2:30
We'll update the /quotes/id put route,
2:32
only this time we'll just
modify the existing code.
2:35
So just before my anonymous
callback function here,
2:38
I'll wrap my async handler
function around it.
2:42
Then I can delete the keyword try along
with its brackets, and this catch block.
2:50
If this feels uncomfortable or
overwhelming, or you just don't feel like
3:00
you're getting exactly what's going on,
don't worry about it.
3:04
When you're practicing
building your own API,
3:07
you can use try catch in every
route until you feel comfortable.
3:09
Or just keep using this syntax and
it'll eventually sink in.
3:12
I encourage you to update all the other
routes using the asyncHandler function,
3:16
to get used to it.
3:20
But also keep in mind this
isn't a necessary step.
3:21
It's just an example of how
you can use middleware to
3:24
save yourself from a bit
of repeating code.
3:27
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up