Nest: Exception wrapper is broken

Created on 31 Mar 2018  路  5Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


When I'm testing type of exception's instance are not equals.
This code should work:

it.only('when user doesn"t exist in the Ldap', async () => {
        service.getUserByUid = jest.fn().mockReturnValueOnce(null);
        await expect(service.removeUser(params.user)).rejects.toBeInstanceOf(UserNotFoundException);
});

When you're using instanceof operator the types are not same.

it.only('when user doesn"t exist in the Ldap', async () => {
        service.getUserByUid = jest.fn().mockReturnValueOnce(null);
        service
          .removeUser(params.user)
          .then(e => {
            console.log(e);
          })
          .catch(e => {
            const x = new UserNotFoundException();
            const er = e instanceof UserNotFoundException;
            const xe = x instanceof UserNotFoundException;
          });
        // await expect(service.removeUser(params.user)).rejects.toBeInstanceOf(UserNotFoundException);
      });
import { NotFoundException } from '@nestjs/common';

export class UserNotFoundException extends NotFoundException {
  constructor(userID: number | string = -1) {
    const msg = `The user: ID - ${userID} not found`;
    super(msg);
  }
}
public async removeUser(userToDelete: UserUpdateDto): Promise<void> {
    const user = await this.getUserByUid(userToDelete.username);
    if (!user) {
      throw new UserNotFoundException();
    }
  }

Expected behavior


e instanceof UserNotFoundException; should be true.

Environment


Nest version: 4.6.6

Most helpful comment

Hi @kamilmysliwiec,

I think that you're misunderstanding about my issue.

The problem is that the exception's instance is not correct. I think that NestJS is altering the exception's instance when is returned from my service (throw new UserNotFoundException).

service
          .removeUser(params.user)
          .then(e => {
            console.log(e);
          })
          .catch(e => {
            const x = new UserNotFoundException();
            const er = e instanceof UserNotFoundException; // er must be true but it's false!!!!
            const xe = x instanceof UserNotFoundException; // this works
          });

working Working hard with Nest! :-)

All 5 comments

Hi @Caballerog,
Exception wrapper has nothing to do with service instances

Hi @kamilmysliwiec,

I think that you're misunderstanding about my issue.

The problem is that the exception's instance is not correct. I think that NestJS is altering the exception's instance when is returned from my service (throw new UserNotFoundException).

service
          .removeUser(params.user)
          .then(e => {
            console.log(e);
          })
          .catch(e => {
            const x = new UserNotFoundException();
            const er = e instanceof UserNotFoundException; // er must be true but it's false!!!!
            const xe = x instanceof UserNotFoundException; // this works
          });

working Working hard with Nest! :-)

Hi @kamilmysliwiec,

I've been searching the possible error until that I've found the mistake in my code....
In the code which I'm testing the import of my custom error was:

import { UserNotFoundException } from '../../commons/exceptions/user-notfound.exception';
import { UserRepeatException } from '../../commons/exceptions/user-repeat.exception';

and the exceptions which I was using in the test code was

import { UserNotFoundException } from '../exceptions/user-notfound.exception';
import { UserRepeatException } from '../exceptions/user-repeat.exception';

So, although the code is same the instances of the exceptions are different (due to the paths are different and the class are different).

In conclusion, the beer and my friends help me! :-D

Hi @Caballerog,
I'm glad that you found a solution! Hope Victoria was tasty and super helpful 馃槃

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

2233322 picture 2233322  路  3Comments

menme95 picture menme95  路  3Comments

KamGor picture KamGor  路  3Comments

tronginc picture tronginc  路  3Comments

anyx picture anyx  路  3Comments