свойство с именем message аргумента HttpException функции catch не имеет statusCode - PullRequest
0 голосов
/ 18 декабря 2018

свойство, называемое сообщением аргумента HttpException функции catch, когда у меня возвращается ошибка 400

[
 {
  "target": {
    "password": "xxxxxx"
  },
  "property": "
  "children": [],
  "constraints": {
    "maxLength": "Este campo no debe tener mas de 30 caracteres",
    "isString": "Este campo debería contener un string",
    "isNotEmpty": "Este campo no puede estar vacío"
  }
 }
]

Но я ожидал получить

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": [
   {
    "target": {
      "password": "xxxxxx"
    },
    "property": "
    "children": [],
    "constraints": {
      "maxLength": "Este campo no debe tener mas de 30 caracteres",
      "isString": "Este campo deberia contener un string",
      "isNotEmpty": "Este campo no puede estar vacío"
    }
   }
  ]
 }

Минимальное воспроизведение проблемыс инструкциями

@Catch(HttpException)
export class ExceptionHandler implements ExceptionFilter {

  catch(exception: HttpException, host: ArgumentsHost) {
    ...
    const statusCode = exception.message.statusCode;//undefined
    ...

    response
    .status(statusCode)//so here there is an error
    .json(json);
  }
}



@Controller('user')
export class UserController {

  @Post('create')
  create(@Body() user: User) {//the error occurred when the user 
                                object do not pass validations
    return user;
  }

  ...
}



export class User {

  readonly id: number;

  @IsNotEmpty({ message: TXT_14 })
  @IsString({ message: TXT_15 })
  @MaxLength(30, { message: TXT_16 })
  readonly username: string;

  @IsNotEmpty({ message: TXT_14 })
  @IsString({ message: TXT_15 })
  @MaxLength(30, { message: TXT_16 })
  readonly password: string;
}

Выдается ошибка

stacktrace

Среда

Версия гнезда: 5.4.0

По вопросам Tooling:

  • Версия узла: 10.9.0
  • Платформа: Mac

Извините за мой сломанный английский:)

...