question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception wrapper is broken

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
Caballerogcommented, Mar 31, 2018

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! 😃

4reactions
kamilmysliwieccommented, Mar 31, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is Java's checked exception handling broken? - Stack Overflow
The problem is that the compiler does not look past method bounds. Within throwsException(), there is no info about E other than "extends ......
Read more >
7 Common Mistakes You Should Avoid When Handling Java ...
Mistake 5: Remove original cause of the exception​​ You sometimes might want to wrap an exception in a different one. Maybe your team...
Read more >
@SneakyThrows - Project Lombok
The code generated by lombok will not ignore, wrap, replace, or otherwise modify the thrown checked exception; it simply fakes out the compiler....
Read more >
Handle errors in ASP.NET Core Blazor apps - Microsoft Learn
Renders error UI when an unhandled exception is thrown. To define an error boundary, use the ErrorBoundary component to wrap existing content.
Read more >
Exceptions | Kotlin
Exception classes · fun main() { //sampleStart throw Exception("Hi There!") //sampleEnd }. To catch an exception, use the try ... catch ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found