Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lo78cn opened this issue Feb 17, 2018 · 5 comments
Closed

Comments

@lo78cn
Copy link

lo78cn commented Feb 17, 2018

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\": \"[email protected]\", \"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\": \"[email protected]\", \"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": "[email protected]",\n  "password": "secret"',
  status: 400,
  statusCode: 400 }

Environment


Nest version: 4.6.3

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

Others:

@chanlito
Copy link

chanlito commented Feb 17, 2018

@lo78cn It's supposed to return 500 error.

@lo78cn
Copy link
Author

lo78cn commented 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.

@kamilmysliwiec
Copy link
Member

Fixed in v4.6.4 🙂

@lo78cn
Copy link
Author

lo78cn commented Feb 19, 2018

Thank you!

@lock
Copy link

lock bot commented Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants