Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Keep a Crash's Details in the Logs

In this lesson, you followed one crash on the wall’s API to both of its ends, the client and the server’s terminal, and made the error handler keep the crash’s details where you can find them.

Key Points

  • A generic 500 alone loses the crash. The last lesson’s handler tells the client nothing internal, and with the planted throw new Error("Simulated save failure") back in, it told you nothing either. The server’s terminal showed only the body line and the timing line, with no stack.
  • For a 500, or any other status from 500 up (that whole range means the server failed), the handler logs the whole error under the request’s ID. Inside if (status >= 500), console.error(res.locals.requestId, error) writes the line as error output and puts the ID that addRequestId() gave the request in front of the error’s first line. The stack follows on the lines under it. The stack’s top line points at the throw, line 50 of routes/problems.ts.
  • The client’s answer doesn’t change. The body still says only that something went wrong, and its X-Request-Id header carries the same ID you saw in the terminal.
  • The stack stays on the server. Ines can’t fix your code with it, and anyone who reads a stack learns your folders and the packages you use. Express’s default page hides it only when NODE_ENV is production. Your handler never sends it.
  • Only server errors are logged. Mei’s request for problem 42 got a 404 with problem_not_found and only its timing line in the terminal. A 404 isn’t a bug on the server, and its body already tells the client what went wrong.
  • The ID finds the crash. Searching the terminal for the ID Ines quotes from her response lands on the error’s first line, the one that starts with her ID. Her stack is right under it, apart from Mei’s and Tunde’s lines. The stack lines themselves carry no ID. Here it’s a handful of lines, but on a busy server it’s thousands.

A crash’s details go to the server’s log, under the request ID, and the client gets a generic message with the same ID.

The Final Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The last lesson ended on a gap. A crash now reaches your error handler, and the client gets a 500 that tells it nothing internal. But the server’s terminal tells you nothing either.

00:12 Plant the same throw before the save, and send Ines’s Moon Walk again. The 500 says only that something went wrong, and the terminal has the body line and the timing line.

00:24 No stack shows up, because your handler replaced the default one that printed it. In this video, you’ll make the server’s end keep what the client never sees.

00:36 The handler holds the whole error, so it decides who gets what. For a 500, or any other status from 500 up, log the error with console.error().

00:47 That whole range means the server failed. And console.error() marks it as error output, so a server’s logs can keep it apart from ordinary lines.

00:56 Put the request ID in front of it. That’s the ID your first middleware gave the request and saved on res.locals. The server restarts. Send Moon Walk again.

01:08 The error’s first line starts with the request ID, and the stack follows on the lines under it. The stack’s top line points at the throw, line 50 of the problems routes.

01:20 The client’s answer hasn’t changed. The body still says only that something went wrong. But its X-Request-Id header carries the same ID you saw in the terminal.

01:32 Why not send Ines the stack as well? It would make debugging easier. But Ines can’t fix your code, and anyone who reads a stack learns your folders and the packages you use.

01:43 Express’s default page hides it only when NODE_ENV is production. Your handler never sends it. So the details stay on the server, and the client gets the ID.

01:55 Mei asks for problem 42, which doesn’t exist, and gets a 404. Her request leaves only its timing line, because the handler logs only server errors. A 404 isn’t a bug on the server, and its body already tells Mei what went wrong.

02:12 Tunde’s search for V4 problems adds one more line. Here, that’s a handful of lines. On a busy server, it’s thousands, which is why you search by ID.

02:24 Now Ines sends you the request ID from her response. Search your terminal for it. The error’s first line starts with her ID, so that’s where the search lands.

02:34 Her stack is right under it, apart from Mei’s and Tunde’s lines.

02:40 So here’s the rule. A crash’s details go to the server’s log, under the request ID, and the client gets a generic message with the same ID. Delete the planted throw, and the wall’s API answers every failure from one place and keeps what you need to fix a crash.

02:58 Next, in Exercise: Answer the Shoe Desk’s Errors, you’ll give the shoe rental shelf the same clear answers.

Become a Member to join the conversation.