Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error logging is called even when handled in custom handler. #208

Open
sean-hernon opened this issue Apr 23, 2022 · 0 comments
Open

Error logging is called even when handled in custom handler. #208

sean-hernon opened this issue Apr 23, 2022 · 0 comments

Comments

@sean-hernon
Copy link

sean-hernon commented Apr 23, 2022

When the error logging is enabled, it is called even when the error is handled in a custom error handler middleware.

This is unintuitive for several reasons. Consider the following case.

//Validation middleware
api.use((req, _, next) => {
    //Assume a function that validates the body and returns a boolean
    if (!validate(req.body)) {
        throw new CustomValidationError();
    }

    next();
});

/*Some routes registered here*/

//Error handling middleware
api.use((err, _, res, next) => {
    //Handle validation errors and send the response
    if (err.name === 'CustomValidationError') {
        return res.status(422).json({reason: 'Some validation reason'});
    }

    next();
});

In this case, the first thing we see in the logs is something like INFO {"level":"fatal",..."statusCode":500}.

Clearly the error is not something that we would consider to be fatal, as we handle it and return a 4XX. Also, the status code says 500, because that's the default in the handling logic and we haven't overridden it at the point at which the log is written, but it's confusing to see these things for a request which is neither fatal nor a 500.

The access log will then be printed with the correct status code, which adds further confusion.

Finally, the readme states that we can "short-circuit" the default error handler by registering a custom one, which I would expect to mean that we only get the logging if we don't register a custom handler, or we call next(), because it seems intuitive that the logging is part of the default handler.

Thank you for taking time to read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant