Categories
Express JavaScript Nodejs

List of Express body-parser Errors

Spread the love

By default, Express 4.x or later doesn’t come with anything to parse request bodies. Therefore, we need to add a library to handle this.

body-parser is a middleware that we can use to parse various kinds of request bodies. It throws several kinds of errors that we need to manage. In this article, we’ll look at what errors it throws.

Errors

When body-parser throws an error, it sends a response code and an error message. The message property of the error has the error message.

The following errors are common errors that are thrown.

Content-Encoding Unsupported

This error is thrown when the request has a Content-Encoding header that contains an encoding but the inflation option was set to false.

The status of the response will be 415m and type property is set to encoding.unsupported. The charset property will be set to the encoding that’s unsupported.

For example, we can reproduce it if we have:

const express = require('express');  
const bodyParser = require('body-parser');  
const app = express();  
const options = {  
  inflate: false,  
};
app.use(bodyParser.urlencoded(options));
app.post('/', (req, res) => {  
  res.send(req.body);  
});
app.listen(3000);

Then when we send a POST request to / with Content-Encoding set to abc and Content-Type set to application/x-www-form-urlencoded, we get:

content encoding unsupported

and a 415 status code.

Request Aborted

This error occurs when the request is aborted by the client before the reading of the body is finished.

The received property will be set to the number of bytes received before the request was aborted.

The status of the response will be 400 and type property set to request.aborted.

Request Entity Too Large

The error will occur when the request body is larger than what’s specified in the limit option.

A 413 error response will be sent with the type property set to entity.too.large.

For example, if we set limit to 0 as follows:

const express = require('express');  
const bodyParser = require('body-parser');  
const app = express();  
const options = {  
  inflate: false,  
  limit: 0  
};
app.use(bodyParser.json(options));
app.post('/', (req, res) => {  
  res.send(req.body);  
});
app.use((err, req, res, next) => {  
  res.status(400).send(err);  
});
app.listen(3000);

Then when we make a POST request to / with a JSON body that has content, then we get:

{"message":"request entity too large","expected":20,"length":20,"limit":0,"type":"entity.too.large"}

Request Size Didn’t Match Content-Length

The request’s length didn’t match the length from the Content-Length header.

This occurs when the request is malformed. Content-Length usually is calculated based on characters instead of bytes.

The status returned will be 400 and the type property is set to request.size.invalid.

For example, if we have:

const express = require('express');  
const bodyParser = require('body-parser');  
const app = express();  
const options = {  
  inflate: false,    
};
app.use(bodyParser.urlencoded(options));
app.post('/', (req, res) => {  
  res.send(req.body);  
});
app.listen(3000);

and we send a POST request to / with the Content-Length header set to -1 and Content-Type set to application/x-www-form-urlencoded, we get a 400 response.

Stream Encoding Shouldn’t Be Set

req.setEncoding method is called prior to body-parser methods will cause this error.

We can’t call req.setEncoding directly when using body-parser.

The status will be 500 and the type property set to stream.encoding.set.

Too Many Parameters

The error occurs when the number of parameters in the request body exceeds what’s set in the parameterLimit.

The response status will be 413 and the type property set to parameters.too.many

Unsupported Charset “BOGUS”

This will occur when the request has a Content-Encoding header that contains an unsupported encoding. The encoding is contained in the message as well as in the encoding property.

The status will be set to 415. The type will be set to encoding.unsupported, and the encoding property is set to the encoding that’s unsupported.

Conclusion

There’re multiple kinds of errors raised by body-parser.

They involve sending bad headers or data that are not accepted by it, or canceling requests before all the data is read.

Various 400 series error status codes will be sent as the response along with the corresponding error messages and stack trace.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.