Specific error message for content-type of JSON but empty body. #698

Merged
merged 2 commits into from Dec 6, 2012

Conversation

Projects
None yet
2 participants
Contributor

evdb commented Nov 29, 2012

As suggested in comments to #680

I spent some time trying to debug this. A more verbose error message would have guided me to the problem straight away.

Tests updated too :)

@tj tj commented on an outdated diff Dec 6, 2012

lib/middleware/json.js
@@ -66,6 +66,13 @@ exports = module.exports = function(options){
req.on('data', function(chunk){ buf += chunk });
req.on('end', function(){
var first = buf.trim()[0];
+
+ if ( buf.length == 0 ) {
@tj

tj Dec 6, 2012

Member

if (0 == buf.length) no jquery parens :D

@tj tj commented on an outdated diff Dec 6, 2012

lib/middleware/json.js
@@ -66,6 +66,13 @@ exports = module.exports = function(options){
req.on('data', function(chunk){ buf += chunk });
req.on('end', function(){
var first = buf.trim()[0];
+
+ if ( buf.length == 0 ) {
+ // content-type says json, but no body. Provide specific error message to help
+ // debug. This is often due to a library not creating valid requests.
+ return next( utils.error( 400, "invalid json - request content-type is 'application/json' but request body empty") );
@tj

tj Dec 6, 2012

Member

same here, also I'd ditch the comment personally since the string describes it already. Maybe change the response to "invalid json, empty body"

Contributor

evdb commented Dec 6, 2012

your wish, my command, etc :)

@tj tj added a commit that referenced this pull request Dec 6, 2012

@tj tj Merge pull request #698 from evdb/master
Specific error message for content-type of JSON but empty body.
be4a240

@tj tj merged commit be4a240 into senchalabs:master Dec 6, 2012

1 check passed

default The Travis build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment