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

BadRequestException 400 getting returned to client as a 500 error #5241

Closed
johncmunson opened this issue Aug 10, 2020 · 7 comments
Closed

BadRequestException 400 getting returned to client as a 500 error #5241

johncmunson opened this issue Aug 10, 2020 · 7 comments

Comments

@johncmunson
Copy link

Bug 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
@johncmunson johncmunson added the needs triage This issue has not been looked into label Aug 10, 2020
@jmcdo29
Copy link
Member

jmcdo29 commented Aug 10, 2020

Please provide a minimum reproduction.

@kamilmysliwiec kamilmysliwiec added needs clarification and removed needs triage This issue has not been looked into labels Aug 11, 2020
@kamilmysliwiec
Copy link
Member

// isoToDate comes from a library that contains shared logic
// for several different Nest apps. This library lists
// @nestjs/core and @nestjs/common as dependencies.

Shared libraries should list @nestjs/core and @nestjs/common as peerDependencies. Otherwise, httpException instanceof HttpException in which httpException is thrown from shared library will give false value.

@v1d3rm3
Copy link

v1d3rm3 commented Mar 27, 2021

I did what you said, but it not worked.

@jmcdo29
Copy link
Member

jmcdo29 commented Mar 27, 2021

@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 like npm link due to Typescript's class instanceof resolution

@v1d3rm3
Copy link

v1d3rm3 commented Mar 28, 2021

@jmcdo29, i'm using Lerna. I'll try to create a reproduction. Thanks.

@v1d3rm3
Copy link

v1d3rm3 commented Mar 30, 2021

@kamilmysliwiec
Copy link
Member

@v1d3rm3 your code is invalid. @nestjs/common and @nestjs/core should be defined as peer dependencies, not dependencies.

@nestjs nestjs locked and limited conversation to collaborators Mar 30, 2021
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

4 participants