1

let's take this snippet for example

const express = require('express')
const bodyParser = require('body-parser')

const app = express()

app.use(bodyParser.urlencoded({ extended: false }))


app.get(...)

# all my routes #

app.use(function (err, req, res, next) {
    console.error(err.stack)
    res.status(500).send('Something broke!')
})

app.listen(9000)
....

if body-parser throws an error?

Maybe the client closes the connection or the request payload is too large

the client will receive an error with status code 500 from the default express error handler but it’s probably a error that should return 4xx response .

I thought of putting a middleware right after it and calling res.send with the error and status code 400.

maybe wrap all my 3rd party middle ware with try-catch handler

Or adding in my error handler middleware a huge switch case And over time adding error thrown from middleware third party middleware and change there status code to 400

All the solutions not really bolt proof and need maintenance over time

What us the best practice any suggestions?

2 Answers 2

1

The body-parser module should call next(err) when it encounters an error and you can then intercept that by providing an Express error handler such as:

app.use(function(err, req, res, next) {
  console.error(err.stack)
  res.status(500).send('Put your content here')
});

When you define this special middleware (with four arguments), then Express will keep track of that separately as an error handler that should be called when/if some request handler calls next(err) or if a synchronous exception is caught by Express.

Express has a default error handler if you do not provide one which you can read about here. But, if you supply one, then yours will take over and you can do whatever you want from the error handler. You can even just log the error and take the user to an error page - you don't have to even send an http error status if it's a web page request. If it's an API, you should probably be using http error statuses.

what is the best practice?

Best practice is to include your own Express error handler where you will be able to intercept all errors that are sent on to next(err).

I thought of putting a middleware right after it and calling res.send with the error and status code 400 or maybe wrap all my 3rd party middle ware with try-catch handler

Your third party middleware needs to behave properly in the Express eco-system. That means that it can throw synchronously and Express will catch it automatically and send to the Express error handler. The code should not throw asynchronously (as is true for all types of code) because nobody can catch those exceptions (unless they are in promise-structured code). You shouldn't have to wrap middleware yourself.

Internal errors should generally not be 4xx errors, but rather 5xx errors. Whether or not you surface an internal error back to the end user depends entirely upon the context. Usually, the user will be shown some sort of general purpose error page. Or, if it's an API, the API will have a scheme for communicating errors and typically the API will return actual http status codes that appropriately map to the type of problem.

Sign up to request clarification or add additional context in comments.

3 Comments

@NaorTedgi - Did this work for you?
You answer was really informative thanks a lot but i am looking for a code implementation example
@NaorTedgi - I provided an example and the documentation link I gave you shows an example. Note sure what else you want?
0

after further investigation, this is the best solution suited for me simply adding another middleware after body parse

https://www.npmjs.com/package/express-body-parser-error-handler

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.