Check out a free preview of the full API Design in Node.js, v4 course

The "Async Error Handlers" Lesson is part of the full, API Design in Node.js, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Scott uses the next function to handle an async error. Calling the next function with the error as a parameter sends the error to the next middleware and allows it to be handled like a synchronous error.


Transcript from the "Async Error Handlers" Lesson

>> Like I said, this only works for synchronous errors. What happens if we have an asynchronous error? What does express catch that? I don't know. Let's figure it out. Let's make something asynchronous. So I'm going to do a, what's the quickest asynchronous thing I can do? I guess I'll just do like a set timeout or something, I guess.

So I'll just say setTimeout for like, one millisecond, and then I'll just say throw new error. So let's see what happens if we if we try to run this. I don't actually know if it's gonna catch that set timeout or not, let's see. Well, we definitely didn't get back what we got last time.

So that's for sure. Look, our server crashed. So Express doesn't know how to handle asynchronous errors because it can't. That's just how JavaScript works. Asynchronous things are happening, like in the future in a different call stack. Express has already bottomed out, that execution call stack has already happened.

And there were no errors in that execution call stack. And then one millisecond later our set timeout happened and an error was thrown. But by then, Express had that stack had already vanished, so there was nothing there. In order to trigger your error handler for asynchronous stuff, you have to handle it yourself.

There's no way around it. You have to be diligent about like, there's gonna be potential error here for this asynchronous code. And then you can tell Express that an error happened. And then you can tap into your middleware. So let's try that, what do I mean by telling Express?

Well if I knew there was gonna be an error here, what I can do instead of throwing it, I could introduce next into a handler. I know before I said handlers don't use next. I lied, they do, only when they have to pass an error. That is it, because before you had an error handler, nothing was next after a handler, it was the final destination of a route.

Nothing came after a handler. Well, error handlers come after handlers now, so you can call next. And the trick is, if you pass anything to next, it treats it like an error. Doesn't matter what you pass in there, it's gonna be an error. So I'm going to put an error in there.

Okay, now, this is going to tell Express that I have an error which will then allow it to be accessed here. Okay, so let's try that. Aha, we're back in business, all right. So you just got to be a little more diligent with asynchronous code. And as you know, we have a ton of asynchronous code.

[LAUGH] So imagine all the places in which you have to think about an error happening, and then how to report that. It can get very tedious. So I guess we're calling that error hello in that and then in our middle word there, if we were to use a template literal, would we be able to show that error in English?

>> Yeah, you could do that. You could just be like had an error and you could say err.essage. Or string they both do the same but.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now