Skip to content

Commit

Permalink
docs(openapi): Add docs to throw different exceptions under the same …
Browse files Browse the repository at this point in the history
…http code
  • Loading branch information
AxelDavid45 committed Feb 5, 2022
1 parent c6e212f commit fe4840f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions content/openapi/types-and-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,24 @@ pets: Pet[];
> info **Hint** The `getSchemaPath()` function is imported from `@nestjs/swagger`.

Both `Cat` and `Dog` must be defined as extra models using the `@ApiExtraModels()` decorator (at the class-level).

#### Multiple responses in controller

In case your app returns different exceptions with under the same HTTP status code, you might use `@ApiExtraModels()` in this way:

```typescript
@Injectable()
export class CatController {
@ApiExtraModels(BadAppCodeException, BadEmailException)
@ApiBadRequestResponse({
schema: {
oneOf: refs(BadAppCodeException, BadEmailException),
},
})
multipleErrorsResponse() { ... }
}
```

First, you tell swagger that `BadAppCodeException` and `BadEmailException` are models it needs to look at. After that, you use `oneOf` and `refs` to set the different exceptions your API might throw.

> info **Hint** The `refs()` function is imported from `@nestjs/swagger`.

0 comments on commit fe4840f

Please sign in to comment.