
Lesson Description
The "Error Handling in Express" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:
Scott explains how to centralize error handling in Express with middleware. Error handlers take four arguments, with the first one always being an error. Once an error is triggered and next() is called, all remaining middleware is skipped, and the request goes directly to the nearest error handler.
Transcript from the "Error Handling in Express" Lesson
[00:00:00]
>> Speaker 1: Error Handling, this is pretty straightforward because it's mostly middleware, but it's a little special We've already been handling our errors somewhat responsibly, doing try-catches and things like that But if you want to format or report your errors to third-party services, log them in other services, it's best to do that in one centralized place
[00:00:00]
So very similar to how we had a centralized place to authenticate our routes, we can do the same thing with error handling using middleware Error Handling, just like middleware, can be used in different places: at the code level, route level, router level, and app level We're going to talk about the middleware version which can be applied to pretty much all levels except the code level
[00:00:00]
The code level is what try-catches are for So how does error handling middleware work in Express It's literally the exact same thing as regular middleware, except it takes 4 arguments, and its first argument is always an error Express will look at the signature of the function to determine if this is an error handling middleware I think that's called the arity of a function—how many arguments a function takes
[00:00:00]
If you register a middleware that takes 4 arguments, Express will recognize it as an Error Handler To trigger the Error Handler, throwing an error won't do that Throwing an error in your middleware or controller will just kill your server If you want to trigger the Error Handler, you have to call Next and pass it an error Technically, you can call Next inside your controllers, although I said you should never do that, but technically you can and have your error handler handle it
[00:00:00]
The problem with calling Next with an error inside a controller is that you're assuming these controllers are being registered to routes with an Error Handler underneath If you call Next with an error inside a controller, it's not going to run another route—it's a terminal route Once you're inside a controller, it's resolved and won't go somewhere else unless there's an Error Handler
[00:00:00]
It's almost like writing a component in React, where you put some assumptions in that component because you know what the parent component will look like This makes your component less flexible—you can't move it around and put it in different places Similar libraries have components like table, table row, and table column components that can't work unless they're inside a specific parent component
[00:00:00]
The error will skip any middleware Once you call Next with an error, everything after that gets skipped—no middleware, nothing else will run It goes straight to the nearest Error Handler Error handlers can also call Next and go to the next Error Handler if you have multiple error handlers registered at different places.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops