Empty @Body() ends up with error 500
See original GitHub issueI’m submitting a…
[ ] Regression
[ ? ] 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
@Post()
method(@Body() myBody: MyBodyDto) {
console.log(myBody);
}
Making a POST without passing any body ends up with an error 500 and the following stacktrace:
TypeError: Cannot read property 'constructor' of undefined
at ValidationExecutor.execute (/home/maxime/Documents/perso/my-project/backend/src/validation/ValidationExecutor.ts:44:90)
at Validator.coreValidate (/home/maxime/Documents/perso/my-project/backend/src/validation/Validator.ts:31:18)
at Validator.validate (/home/maxime/Documents/perso/my-project/backend/src/validation/Validator.ts:56:21)
at Object.validate (/home/maxime/Documents/perso/my-project/backend/src/index.ts:49:44)
at ValidationPipe.transform (/home/maxime/Documents/perso/my-project/backend/node_modules/@nestjs/common/pipes/validation.pipe.js:43:45)
at transforms.reduce (/home/maxime/Documents/perso/my-project/backend/node_modules/@nestjs/core/pipes/pipes-consumer.js:15:28)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
Note: I’m using class-transformer
and class-validator
. My DTO is like that for example:
export class MyBodyDto {
@IsUUID('4')
@IsNotEmpty()
id: string;
}
Expected behavior
If an argument is not optional and is not present when a request arrives, the app shouldn’t crash and end up with a 500 error.
Minimal reproduction of the problem with instructions
CF code above
Environment
Nest version: 5.0.1
For Tooling issues:
- Node version: 10
- Platform: Linux
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Getting 500 Internal server error with the empty body in the ...
Getting 500 Internal server error with the empty body in the endpoint. Please see below the order request and response:.
Read more >Internal Server error 500 : No file specified in the request body
I am using and API that accepts an image file and returns a JSON string as a response on console C# application.
Read more >500 Internal Server Error - HTTP - MDN Web Docs - Mozilla
This error response is a generic "catch-all" response. Usually, this indicates the server cannot find a better 5xx error code to response.
Read more >How to Fix a 500 Internal Server Error on Your WordPress Site
The 500 Internal Server Error status code occurs when the server encounters an error that prevents it from fulfilling the request.
Read more >HTTP Error 500 – Internal Server Error Explained in Plain ...
Let's imagine you are accessing a website using a Laravel PHP API for its back end. The thing throwing the 500 error could...
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 Free
Top 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
@maxime1992 it’s perfectly fine. This exception is thrown from
class-validator
when the values passed in isundefined
/null
. I’ll add a small improvement soon. At this point, you may catch this issue with the exception filter.Fixed in the v5.1.0