Exception wrapper is broken
See original GitHub issueI’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:
- Created 5 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
).Working hard with Nest! 😃
Hi @Caballerog, I’m glad that you found a solution! Hope Victoria was tasty and super helpful 😄