question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SyntaxError: Unexpected end of JSON input returns statuscode 500 instead of 400

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

curl -X POST "http://localhost:3000/login" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"email\": \"user@domain.com\", \"password\": \"secret\""

{
  "statusCode": 500,
  "message": "Internal server error"
}

Expected behavior

curl -X POST "http://localhost:3000/login" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"email\": \"user@domain.com\", \"password\": \"secret\""

{
  "statusCode": 400,
  "message": "Bad request"
}

Or even better:

{
  "statusCode": 400,
  "message": "SyntaxError: Unexpected end of JSON input"
}

Minimal reproduction of the problem with instructions

Boilerplate nestjs application, any post endpoint, any invalid json POST.

What is the motivation / use case for changing the behavior?

If I enable my global AnyExceptionFilter and log the exception, I get a 400 statuscode.

{ SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at parse (/Users/name/Downloads/avgAPI/node_modules/body-parser/lib/types/json.js:88:17)
    at /Users/name/Downloads/avgAPI/node_modules/body-parser/lib/read.js:116:18
    at invokeCallback (/Users/name/Downloads/avgAPI/node_modules/raw-body/index.js:262:16)
    at done (/Users/name/Downloads/avgAPI/node_modules/raw-body/index.js:251:7)
    at IncomingMessage.onEnd (/Users/name/Downloads/avgAPI/node_modules/raw-body/index.js:307:7)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1056:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
  body: '{\n  "email": "user@domain.com",\n  "password": "secret"',
  status: 400,
  statusCode: 400 }

Environment


Nest version: 4.6.3

 
For Tooling issues:
- Node version: 8.9.1  
- Platform:  Mac 

Others:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Feb 18, 2018

Fixed in v4.6.4 🙂

1reaction
lo78cncommented, Feb 17, 2018

@chanlito I disagree 😃

Internal Error 500
The server encountered an unexpected condition which prevented it from fulfilling the request.

It is not an unexpected error.

Bad request 400
The request had bad syntax or was inherently impossible to be satisfied.

Makes more sense in my opinion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught (in promise) SyntaxError: Unexpected end of JSON ...
I am trying to send a new push subscription to my server but am encountering an error "Uncaught (in promise) SyntaxError: Unexpected end...
Read more >
“Unexpected end of JSON input” – possible fixes - LinuxPip
is a common error message in JavaScript, occurs when the user tries to convert an invalid JSON string into a native JS object...
Read more >
Unexpected end of JSON input Error in JavaScript | bobbyhadz
The "Unexpected end of JSON input" error occurs when trying to parse invalid JSON using the JSON.parse or $.parseJSON methods. Trying to parse...
Read more >
Fixing a 500 internal server error response - Postman
A 500 Internal Server Error is an HTTP status code that indicates that the server encountered an unexpected error while processing the ...
Read more >
How to Fix a 400 Bad Request Error (Causes and Fixes) - Kinsta
URL String Syntax Error · Corrupted Browser Cache & Cookies · DNS Lookup Cache · File Size Too Large · Generic Server Error ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found