SyntaxError: Unexpected end of JSON input returns statuscode 500 instead of 400
See original GitHub issueI’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:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in
v4.6.4
🙂@chanlito I disagree 😃
It is not an unexpected error.
Makes more sense in my opinion.