Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- What is Express Middleware? 2:22
- Middleware Sequence and Routing 5:11
- Review: Middleware Sequence and Routing 5 questions
- The `next` function and a Closer Look at Middleware 4:45
- Handling Errors in an Express App 3:56
- Review: `next` and Handling Errors 5 questions
- Error Handling Middleware 5:20
- Handling 404 Errors 2:34
- Review: Error Handling and 404s 5 questions
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Handling errors is an important (and easy to overlook) topic any program should address. Learn how to handle them in Express.
More on the JavaScript Error constructor
- Error (MDN article)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
vamosrope14
14,932 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Remember back at
the beginning of the course,
0:00
when we made our first request of the app.
0:02
The client received a 404
error back from our route.
0:05
This meant, Express didn't have
the page that we requested.
0:10
More specifically,
it meant that there was no get route for
0:13
our app matching the URL we requested.
0:18
Applications always need a way
of handling errors like this.
0:21
Other errors might occur when
the app can't reach a database, or
0:26
when a user mistype their password.
0:31
Errors are an important tool for
0:34
your user as they learn how
to interact with your app.
0:36
They offer information about
the limits of how your app can be used.
0:41
Errors also guide developers in
fixing bugs in an application.
0:46
In Express, you can use the next
function to signal an error in your app.
0:50
By passing an object as
a parameter to next,
0:55
Express knows there is an error to handle.
0:59
Express will then immediately
terminate and handle the error.
1:03
Let's create our own error.
1:08
We'll use the new error constructor,
And pass in a string.
1:15
The string will serve as an error message.
1:24
Normally, no, wouldn't be a good
error string in a real app,
1:30
because it doesn't provide any
clues about what went wrong.
1:34
But as you'll see soon there is a lot
of text when an error is thrown, and
1:37
hopefully this text will be easy to
pick out when we view it on the browser.
1:42
This is JavaScript's
native error constructor.
1:48
If you haven't seen this syntax before or
1:51
haven't seen it too often,
don't worry about it for now.
1:54
It's enough to know that we're creating a
custom error object and storing it in ERR.
1:58
Look in the teacher's notes if you'd
like to read more about JavaScript's
2:05
error constructor.
2:09
Let's pass in the error object as
an argument to the next function call.
2:11
If I save it and refresh the browser,
there is a lot of text here.
2:17
The error is thrown and at the top, you
can see the error message was outputted.
2:22
The next line tells us that
the error was thrown from line 14.
2:30
The second number is the column, or
2:37
how many spaces from the first
position on line 14.
2:40
Pinpointing where an error is in an app
can really speed up the process of
2:46
fixing bugs.
2:50
All of the lines after this indicate
deeper layers of the Express framework,
2:52
and you probably won't
need to go into this.
2:56
Sometimes though, if you really stumped,
2:59
pasting this into a Google search
can get you closer to your answer.
3:02
Let's check the console.
3:07
And you can see Hello is logged out
just above where the error was thrown.
3:11
World never gets logged, because
the program was interrupted by the error.
3:17
Let's switch back to the browser.
3:25
This output is coming from
Express's built in error handler.
3:28
It has some good information in here,
but the formatting has some room for
3:32
improvement.
3:37
More readable error messages can
make development an easier process.
3:39
Express offers a way to write
our own custom error handlers,
3:44
where we can format the errors
to be much more readable.
3:48
I'll show yo how to do that next.
3:53
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