API Design in Node.js, v4

Handling Errors with process.on()

Scott Moss

Scott Moss

Initialized
API Design in Node.js, v4

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

The "Handling Errors with process.on()" 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 explains how errors can be handled outside of Express using the process.on API. Similar to using addEventListener, the "on" method has named events like uncaughtException and unhandledRejection which will trigger when certain errors are thrown.

Preview
Close

Transcript from the "Handling Errors with process.on()" Lesson

[00:00:00]
>> Okay, the last thing I want to show for error handling. And I did put this on the slides because actually it doesn't have anything to really do with API specifically. But has everything to do with node. So I want to talk about it, which is, we talked about handling errors inside of express.

[00:00:16]
What if you have an error outside of Express but in node, not in handler. Well, it'll just break so you can try catch it, if you just throw an error you saw it throw an error at the top of that file I ran a file it broke. You can just try catch that but for an asynchronous error that happens somewhere later in your app that's not an express.

[00:00:33]
How do you handle that well, you can get away with, you can use something called process. So it's a really good intuitive process I'll make another file here called error Jas. I'm going to do a set timeout for like 300 milliseconds and I went through an error. And I'm gonna say oops, okay?

[00:01:04]
Throw that error and I'm just gonna run this file. So let's see what happens. Node, error JS, on three miliseconds and then boom. It says oops. Okay. We didn't handle the error because a synchronous. But what you can do is you can tell, you can use this built in thing called process that's built into node.

[00:01:25]
And if you don't have any experience with operating system level environmental languages like Node JS or Python. Anything that you can run in the terminal basically, there's a thing called process right. Process is an object, at least in JavaScript that represents this the current process in which you're in.

[00:01:46]
The file system, the current file, all the variables, the secrets, every piece of metadata associated with the process you are in. The hardware you're on, the capabilities of that hardware, everything IP address, things like that. So the process object and node has a bunch of stuff on it.

[00:02:04]
I mean, if you just hit dot and scroll through it. Yeah, I'm still scrolling. It's just like a infinite scroll. So, you can do process dot on or on hand or process dot on then uncaught exception or unhandled rejection. So process that on is basically an event handler so you can listen for certain things.

[00:02:36]
Uncaught exception is like, there was an error that was thrown that no one caught. I can handle it here. And then the same is true for something asynchronous, I can say for uncaught rejection. Which is like an asynchronous error is happening and I promise I can handle it here as well.

[00:03:04]
So if something breaks, sometimes you'll see an error. And note I'm like, this errored it out unhandled rejection. I don't know what the error is. I just know that it did error because it's asynchronous, I don't know what it is, but it broke. You would put this in your app somewhere to catch it.

[00:03:22]
Like at the top of your index file or something like that, just to register this. And this has nothing to do with Express. This is just catching stuff and node.

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