nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.89k stars 7.56k forks source link

Error pass object parameter in HttpException #372

Closed biplabmalakar closed 6 years ago

biplabmalakar commented 6 years ago

I'm submitting a...

[ ] Regression [ ] Bug report [ ] Feature request [x ] Documentation issue or request [ ] Support request => Please do not submit support request here, instead post your question on Stack

Current behaviour

Pass object to the HttpException is not working, because its giving error TSError: ⨯ Unable to compile TypeScript src\demo\demo.middleware.ts (14,23): Expected 2 arguments, but got 1. (2554) at getOutput (E:\project\node_modules\ts-node\src\index.ts:307:15) at E:\project\node_modules\ts-node\src\index.ts:336:16 at Object.compile (E:\project\node_modules\ts-node\src\index.ts:498:11) at Module.m._compile (E:\project\node_modules\ts-node\src\index.ts:392:43) at Module._extensions..js (module.js:646:10) at Object.require.extensions.(anonymous function) [as .ts] (E:\project\node_modules\ts-node\src\index.ts:395:12) at Module.load (module.js:554:32) at tryModuleLoad (module.js:497:12) at Function.Module._load (module.js:489:3) at Module.require (module.js:579:17)

If I pass object and the status to the HttpException then work, like throw new HttpException{object, HttpStatus.PRECONDITION_FAILED }

Minimal reproduction of the problem with instructions

# error.list.ts import { HttpStatus } from '@nestjs/common'; export class ErrorList { public List = { 'internal_error': { status: HttpStatus.INTERNAL_SERVER_ERROR, code: 'iternal_error', message: 'Internal Server error' }, 'data_not_found': { status: HttpStatus.NOT_FOUND, code: 'resource_not_found_error', message: 'Data not found' }, 'invalid_path': { status: HttpStatus.BAD_GATEWAY, code: 'invalid_path_error', message: 'Invalid path request', }, 'basic_auth': { status: HttpStatus.PRECONDITION_FAILED, code: 'request_validation_error', message: 'Unknown source' } } }

createError.component.ts

import { ErrorList } from './../Response/errorList.list'; import { Component } from '@nestjs/common';

@Component()

export class createError extends ErrorList { constructor() { super(); } create(code: any = 'internal_error', message: any = null) { let error = this.List[code]; if (!message) return error; else return error.message = message; } }

in demo.controller.ts @All() async notFound( @Req() request, @Res() response) { console.log('Inside'); let error = this.E.create('invalid_path'); throw new HttpException(error); }

Environment

ericzon commented 6 years ago

Documentation has a similar issue:

In https://docs.nestjs.com/exception-filters / HttpException category, in the second cats.controller.ts, the doc says:

@Post()
async create(@Body() createCatDto: CreateCatDto) {
  throw new HttpException({
    status: HttpStatus.FORBIDDEN,
    error: 'This is a custom message',
  });
}

It lacks of second argument, I think is outdated. In the 01-cats-app repository is ok.

kamilmysliwiec commented 6 years ago

The docs are misleading in this case. You need to pass HTTP status number as a second value 🙂

kamilmysliwiec commented 6 years ago

Fixed in the new documentation: https://docs.nestjs.com/v5 I'll merge it soon.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.