Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed REST APIs with Express!
You have completed REST APIs with Express!
Preview
Currently, we have no way to catch errors using async/await. In this video we'll learn how to use Node's try/catch block to catch and handle errors.
This video doesn't have any notes.
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
At present, to use async and
await with Express,
0:00
we need some way to capture any errors
that might occur in our await function,
0:03
like if something goes wrong with the
server when we try to create a new quote.
0:07
Let's take a look at what
happens if an error occurs now.
0:10
Above my code,
0:13
I'll throw a fake error using
JavaScript's built-in error constructor.
0:14
If we go back to Postman and
try to create a new quote again,
0:28
you can see that our API isn't responding.
0:33
It will just hang indefinitely and
that's not what we want.
0:36
To address this, we need to wrap our
code in a JavaScript try-catch block.
0:39
All the code we've written in this
route so far goes into the try block.
0:55
The catch block takes one parameter,
1:05
which represents whatever
error message it catches.
1:07
Inside the catch block,
1:12
we can send that message back to
the client in the form of JSON.
1:13
For example, let's mimic if
1:19
something went wrong by throwing this
1:23
fake error inside of the try block.
1:28
So what should happen is an error will be
thrown and this message will fall into
1:35
the catch block and it will be
sent back to the client as JSON.
1:40
So let's save, make sure the server
is running, and go back to Postman.
1:43
We'll try to create the quote again, and
1:48
now you see we're being
sent an error message.
1:51
Notice, however, that we're still
getting back a status code of 200 OK,
1:55
even though something
obviously went wrong.
1:59
When something goes wrong, we wanna
make sure we're sending accurate HTTP
2:02
status codes, as well as communicating
clear errors to the client.
2:05
We'll talk about that in the next video.
2:10
I'll go ahead and
erase the error we just made.
2:13
And let's do this one more time by
refactoring our route handler that gets
2:17
a single quote to use try-catch.
2:20
I'll start by creating a try-catch block.
2:23
Remembering to pass in the error.
2:31
We'll copy and
paste our code into the try block.
2:35
And now in the catch block,
in the event of an error,
2:41
I can send back some JSON
containing an error message.
2:44
For additional practice,
I recommend going back and
2:56
refactoring this first route
to use try-catch as well.
2:59
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