Error Handling in Express (FCC Youtube course) - Clarifications

Hi Everyone

So, in order to learn node.js and express, I’m following along with this course on the FCC youtube channel, and I wanted to clarify something on the Express error handling.

In the express docs, an “error handler” in Express is a midware that has four parameters:

Error-handling middleware always takes four arguments. You must provide four arguments to identify it as an error-handling middleware function. Even if you don’t need to use the next object, you must specify it to maintain the signature. Otherwise, the next object will be interpreted as regular middleware and will fail to handle errors.

In the above course, the instructor (John Smilga) creates two error handlers, one is a general error-handler, and the other a “not found” error handler. This is the first one, with four params as per the docs:

const errorHandlerMiddleware = async (err, req, res, next) => {
  console.log(err)
  return res.status(500).json({ msg: 'Something went wrong, please try again' })
}

However, this is the “not found” error handler - only two params (three, with next() omitted):

const notFoundMidware = (req, res) => {
    return res.status(404).json({ msg: "Products Not found." });
};

And this works, whenever a product isn’t found. More than that - when I rewrote it with four params, to make it similar to the Error-Handler above, it didn’t work. It only works as a regular router, though it’s supposed to be an error handler.

This is seems very weird. Could anyone please clarify?

This is how the midwares are mounted in my app.js:

app.get("/", (req, res) => {
    return res.status(200).send(`<h1>Store API</h1><a href="/api/v1/products">Products Page</a>`);
});

app.use("/api/v1/products", prodRouter);

//Middleware
app.use(express.json());
app.use(errorHandlerMiddleware);
app.use(notFoundMidware);

share that attempted code, thanks :slight_smile:

1 Like

Haha of course:

const notFoundMidware = (err, req, res, next) => {
    return res.status(404).json({ msg: "Products Not found." });
};

Essentially the same, with four parameters instead of two, as per the docs. It’s supposed to act as an error handler now, but it doesn’t. Not sure why.

you sure about that? specially about its “chronology”

cause what i have seen so far there are three parameters in middleware functions namely (req, res, next), also be sure about its “order” as well, thats really important, otherwise you woiuld be trying something which it cant do or perform

share that docs link if you would like, happy learning :slight_smile:

Hello there,

I am not sure about this, but this is my understanding:

Registering a middleware whose signature expects 4 arguments tells Express to use that function whenever the callback of a route throws.

Registering a middleware whose signature does not expect 4 arguments tells Express to use that function with every event (request).

So, notFoundMidware should always be called. Have you tested this?
And, errorHandlerMiddleware should only be called if, within a defined route, an error is thrown.


The Express docs also say this:

You define error-handling middleware last, after other app.use() and routes calls; for example:

However, I did not think this to be a “rule” :man_shrugging:

Hope this helps