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
500alone loses the crash. The last lesson’s handler tells the client nothing internal, and with the plantedthrow 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. Insideif (status >= 500),console.error(res.locals.requestId, error)writes the line as error output and puts the ID thataddRequestId()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 ofroutes/problems.ts. - The client’s answer doesn’t change. The body still says only that something went wrong, and its
X-Request-Idheader 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_ENVisproduction. Your handler never sends it. - Only server errors are logged. Mei’s request for problem 42 got a
404withproblem_not_foundand only its timing line in the terminal. A404isn’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.