Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

typescript - How to handle mongoose error with nestjs

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, you forgot to add return in your create method inside the controller. This is a common, very misleading mistake I made a thousand of times and took me hours to debug.

To catch the exception:

You could try to catch MongoError using @Catch.

For my projects I'm doing the following:

import { ArgumentsHost, Catch, ConflictException, ExceptionFilter } from '@nestjs/common';
import { MongoError } from 'mongodb';

@Catch(MongoError)
export class MongoExceptionFilter implements ExceptionFilter {
  catch(exception: MongoError, host: ArgumentsHost) {
    switch (exception.code) {
      case 11000:
        // duplicate exception
        // do whatever you want here, for instance send error to client
    }
  }
}

You can then just use it like this in your controller (or even use it as a global / class scoped filter):

import { MongoExceptionFilter } from '<path>/mongo-exception.filter';

@Get()
@UseFilters(MongoExceptionFilter)
async findAll(): Promise<User[]> {
  return this.userService.findAll();
}

(Duplicate exception doesn't make sense here in a findAll() call, but you get the idea).

Further, I would strongly advise to use class validators, as described here: https://docs.nestjs.com/pipes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...