John Schmitz
John Schmitz

Reputation: 118

Throwing HTTP nest Exception results in Promise rejection

I'm trying to throw a http nest exception to make use of the default nest handler.

I Tried to throw the exception in the controller that calls the adminService.update function, and with great success it worked.

  async update(update: DeepPartial<Admin>) {
    const admin = await this.findOne({ id: update.id});
    const adminName = await this.findOne({ username: update.username});
    if (!adminName) {
      throw new ConflictException('Username already in use');
    }
    admin.username = update.username;
    admin.save();
  }

Output when putting the call in the controller:

{
  "statusCode": 409,
  "error": "Conflict",
  "message": "Username already in use"
}

The controller method.

  @Put()
  async update(@Body() updateDTO: UpdateDTO): Promise<void> {
    throw new ConflictException('Username already in use');
    this.adminService.update(updateDTO);
  }

The error itself:

UnhandledPromiseRejectionWarning: Error: [object Object] at AdminService.<anonymous> (C:\Users\JBRETAS_EXT\Documents\mapa-digital\dist\admin\admin.service.js:55:19) at Generator.next (<anonymous>)

Upvotes: 0

Views: 1320

Answers (1)

John Schmitz
John Schmitz

Reputation: 118

It seems that I was missing a return statement in my controller method.

  @Put()
  async update(@Body() updateDTO: UpdateDTO): Promise<void> {
    return this.adminService.update(updateDTO);
  }

Upvotes: 3

Related Questions