I followed the example from https://docs.nestjs.com/techniques/mongodb
The issue is when there is a mongoose validation error (e.g i have a schema with a required field and it isn't provided):
From games.service.ts:
async create(createGameDto: CreateGameDto): Promise<IGame> {
const createdGame = new this.gameModel(createGameDto);
return await createdGame.save();
}
The save() function returns a Promise.
Now i have this in the game.controller.ts
@Post()
async create(@Body() createGameDto: CreateGameDto) {
this.gamesService.create(createGameDto);
}
What is the best way to handle an error and then return a response with a different http status and maybe a json text?
You would usually throw a HttpException
but from where? I can't do that if i handle the errors using .catch() in the promise.
(Just started using the nestjs framework)