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.

BadRequestException 400 getting returned to client as a 500 error

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcdo29commented, 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

1reaction
kamilmysliwieccommented, Aug 11, 2020

// 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.

Read more comments on GitHub >

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

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