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.

Empty @Body() ends up with error 500

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
kamilmysliwieccommented, May 28, 2018

@maxime1992 it’s perfectly fine. This exception is thrown from class-validator when the values passed in is undefined/null. I’ll add a small improvement soon. At this point, you may catch this issue with the exception filter.

1reaction
kamilmysliwieccommented, Jul 5, 2018

Fixed in the v5.1.0

Read more comments on GitHub >

github_iconTop 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 >

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