Example values from default nestjs exceptions are null
Morishiri opened this issue ยท 4 comments
First of all thanks for this decorator, becuase it is exactly what I looked for, however I'm not able to get it to work correctly..
Here is what I managed to achieve:
by
@ApiOperation({ summary: 'Create user' })
@ApiBody({ type: CreateUserDto })
@ApiException(BadRequestException)
@ApiResponse({
status: 201,
description: 'The created user',
type: User,
})
@Post()
async create(@Body() userData: CreateUserDto): Promise<User> {
Is there anything I did wrong here or it should not work that way? I expected the example there to say:
{
"statusCode": number,
"message": "string",
"error": "string"
}
Hi @Morishiri,
the decorator works as expected. I've to admit, that null
is a bad "Example Value". This happens, because there is no description defined. For example:
@ApiException(BadRequestException, { description: 'Could not create user' })
When specifying the description
the exception will be shown like in this example: https://nanogiants.github.io/nestjs-swagger-api-exception-decorator/apiexception/examples/default
I see that NestJS handles exceptions and returns
{
"statusCode": number,
"message": string
}
as default response. I'm going to fix that in the next version. The next version then will use the default NestJS exception response when no template has been specified.
If you don't want to wait or you're using a NestJS exception filter, you could use the template
option or the templated ApiException decorator to specify your preferred "Example Value" template. We've explained this in the documentation: https://nanogiants.github.io/nestjs-swagger-api-exception-decorator/gettingstarted/usage/templated
If you have any further questions, please feel free to contact us again ๐
Hi @Morishiri,
I wanted to let you know, that we've just released a new version containing the default NestJS exception response body template when handling exceptions ๐
That was fast :D Thanks!