BadRequestException 400 getting returned to client as a 500 error
See original GitHub issueBug Report
Current behavior
I have an endpoint that is returning 500 when it should be returning 400. The controller associated with this endpoint is throwing a BadRequestException, but when it bubbles up to the global Nest error handling mechanism it is not being recognized as an instance or subclass of HttpException. Therefore, Nest is returning a default of 500 to the client.
import { isoToDate } from 'shared-nest-logic';
@Controller('todos')
class TodosController {
constructor(private readonly todoService: TodoService) {}
@Get()
async getTodos(
@Query('start') start: string,
@Query('end') end: string
) {
// isoToDate comes from a library that contains shared logic
// for several different Nest apps. This library lists
// @nestjs/core and @nestjs/common as dependencies.
// When a required query parameter is missing, isToDate will
// throw a NestJS BadRequestException.
const [startDate, endDate] = [isoToDate(start), isoToDate(end)];
const todos: Todo[] = this.todoService.getTodos(startDate, endDate);
return todos;
}
}
Expected behavior
I would expect Nest to recognize the BadRequestException as a valid instance of HttpException and return 400 to the client, rather than the fallback error of 500.
Environment
Application:
@nestjs/common: 7.3.2
@nestjs/core: 7.3.2
Library:
@nestjs/common: 7.3.2
@nestjs/core: 7.3.2
- Node: 10.16.3
- NPM: 6.9.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to Fix a 400 Bad Request Error (Causes and Fixes) - Kinsta
The 400 Bad Request error indicates that the server cannot or process the request due to a client error. Read about the common...
Read more >400 Bad Request Error: What It Is and How to Fix It
The 400 Bad Request Error is an HTTP response status code indicating that the server was unable to process the request sent by...
Read more >Return bad request (400) or internal server error (500)
If the body of your request was missing some property, it is a client error and should be in the 4xx range of...
Read more >400 Bad Request - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request ......
Read more >Handling Exceptions Returned from the Web API
You'll learn to how to return 500, 404, and 400 exceptions and how to ... the table on the Web page, a 500...
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
@v1d3rm3 without a reproduction I can’t tell you what’s wrong. What Kamil said it’s the correct approach. If you’re working with this locally, you’ll need to move the built library to the
node_modules
rather than using something likenpm link
due to Typescript’s class instanceof resolutionShared libraries should list
@nestjs/core
and@nestjs/common
as peerDependencies. Otherwise,httpException instanceof HttpException
in whichhttpException
is thrown from shared library will givefalse
value.