свойство, называемое сообщением аргумента 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;
}
Версия гнезда: 5.4.0
По вопросам, связанным с инструментами:
Извините за мой ломаный английский :)
В предыдущем api, который я создал с помощью nestjs 5.4.0, встроенный ValidationPipe вызывал исключение HttpException, но в последней версии этого не делает, поэтому мне пришлось создать свой собственный ValidationPipe, но спасибо за ваш ответ и время !! :)
Вы используете Nest, встроенный в ValidationPipe? Просто использовать @Body недостаточно, чтобы проверка происходила так