Check out a free preview of the full The Hard Parts of Servers & Node.js course

The "Error Handling in Node" Lesson is part of the full, The Hard Parts of Servers & Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Will explains how Node's background HTTP features determine which function to auto-run, specifically for errors instead of requests.

Preview
Close

Transcript from the "Error Handling in Node" Lesson

[00:00:03]
>> Will Sentance: Handling errors. We get errors in server side development. Look, I'm dealing with someone else's computer, Michael's, trying to send messages to my computer. There's 1,000 things that could go wrong in that process. We need to be able to handle errors. In server side development, we got errors.

[00:00:22]
This is the server side, the client side, the server side, we get errors. Understandable, we are interacting with others' computers, good apostrophe placement there. [LAUGH]
>> Will Sentance: Little tip here, always just, whatever. With the computers of others, put it that's how you figure out the, others' computers over the Internet.

[00:00:47]
There's a lot of issues that could arise. How do we handle this? We need to understand better how our background HTTP feature is working. Right now it only auto triggers doOnIncoming, when the message comes in. But what if we get a corrupt request message arriving? Do we want to look at it, investigate it, and send something back?

[00:01:15]
No, we want to look at it, but log it, probably to a console, and see what error is at hand. That means we probably don't wanna run this function. Wouldn't it be nice if we could set up another function that will be the one that runs when an error shows up, or a client error?

[00:01:36]
The client is sending an error, a problematic URL, corrupted data in some format. We wanna have a separate function. How can we indicate which one we want to actually run? Turns out, people, there's a little piece of Node here that I did not tell us about before. Which is that the auto run function doOnIncoming, actually auto runs, let me rephrase that.

[00:02:02]
When the inbound message arrives, it's not automatically running this function. It actually sends out a loud shout within Node. A message, they call it an event, just a word that gets shouted out, broadcast, emitted, in Node. And that word that gets shouted out, and this is a built-in word, we don't choose this.

[00:02:24]
That word that gets shouted out when a message arrives is, request. [SOUND] It shouts out request. And that is what triggers, as someone awkwardly cleared their throat after that as an empathetic response to my slightly strangled cry, that is what triggers this function. How do we tell Node that we want to have this function triggered on that word that gets broadcast, flashed out?

[00:02:58]
Well, we actually did that implicitly, with passing doOnIncoming to createServer. That said, hey, Node, when you flash out the word request run doOnIncoming. But we can actually also do it manually, people. Check this out. Node will automatically send out the appropriate event. These are the broadcast messages that screams out within Node when the right thing happens.

[00:03:25]
So in comes a message, it's a good one, it's gonna flash out the word request. Now if we pass the function to create server, that will actually, what it'll do is set, if you see word request come in, run doOnIncoming. But we can actually also set that up manually.

[00:03:41]
Here, we're just gonna set up the create with a server in the background. We're not going to pass doOnIncoming at that moment, we're gonna do it manually. We're going to set up the server and then use another function, that's another edit function. And we're gonna use auto run, and we're gonna pass it the word that we know will be flashed out, and the function we want to auto run on that word being flashed out.

[00:04:08]
On request, we want it to auto-run doOnIncoming. If we get an error message coming in, though, a bad request, Node's gonna look at it and go uh-oh, and it's not gonna flash out requests at all. It's not gonna broadcast the word request within Node. It's not gonna emit the event.

[00:04:24]
They all mean the same thing, request within Node. It's gonna emit the event, what, Michael, what is it likely to emit? What do you think it's gonna emit? What's the word, Sam, that he's gonna flash out on error?
>> Sam: Error.
>> Will Sentance: Actually under the hood, it does, but it packages up, Matt, as?

[00:04:43]
>> Matt: Client error.
>> Will Sentance: Client error. It's gonna flash out automatically if we get an error coming in. And we'll see this all before, we wouldn't worry. Client error. And we therefore need to say, if you hear the word client error flash out, don't run doOnIncoming, run, looks like we're gonna say it, run doOnError, and that's what we're gonna do.

[00:05:05]
And that's what we're gonna do. All right, here we go, people. We're gonna do it all from scratch again. So let's take a picture of these extraordinary ramblings of a mad person. Okay, here, I don't want to use this. Here we go, folk, here we go. We're going to do it all from scratch.

[00:05:26]
I'm gonna be calling on all of you. This is the last time, yeah, I'm gonna rub my forehead, too. Exactly, William's right, draining, exhausting, but if we get this down we're good for life. You saw this. You saw this in the challenge how it already comes back to that auto run function, auto inserted data.

[00:05:45]
Set up the background feature, let's do it. After this, people, just so we know what's coming, we're gonna use a different background feature from the computer, the file system. And then the only last thing we've got to cover is whether these auto run functions actually allow back in, at what moment.

[00:06:03]
And that's going to introduce us to the event loop, the cues. Some more live you v stuff, all to come. All right, here we go. We're still not quite done, but it'll do, okay. So here we are, Twitter's computer that we're writing code for Twitter's computer. Then in orange was, whose do we have this time?

[00:06:31]
Charlie's computer. I'm gonna say, I can already start to feel the tiredness of doing the same damn thing again and again. Okay, I can feel it. I'm gonna start doing the thing where my head leans against the whiteboard. Excellent, Charlie's computer, there it is. And now exactly as Andrew said, the computer,

[00:06:52]
>> Will Sentance: Internal features,
>> Will Sentance: There they are. Now you might say, why do we just not have these up here permanently? That's a good question.
>> Will Sentance: That's the end of that, that's a good question. All right, and then we had Node C++ features, Node C++ features in the famous color of Node Purple, all right?

[00:07:22]
And then we had a JavaScript runtime. This bit up here is a JavaScript runtime. It's the place in which you can run JavaScript code. Okay, perfect.

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