Nestjs expected to throw customException but always throws default InternalServerErrorException

Viewed 197

I have a dynamic module that was registered in the app module. It has a utility class called PermissionsUtil, In which it handles certain validation logic and throws UnauthorizedException, But I always get InternalServerErrorException at the receiver end.

Inside Dynamic Module

permissions.util.ts

@Injectable()
 class PermissionsUtil {
      
   someFunction() {

       ...performs validation logic here

        if(badCase) {
         throw new UnauthorizedException('Insufficient Permission');
      }
      return true;
    }
  }

Inside app Module

some.controller.ts

@Controller()
 class SomeController {
constructor(
    private readonly permissionsUtil: PermissionsUtil,
  ) {}
   @Patch()
   someFunction() {
     await this.permissionsUtil.someFunction(); //here it should throw unAuthorizedException but throws default InternalServerErrorException
      return true;
    }
  }

Note: All modules, controllers, and service registrations were correct and working as expected.

0 Answers
Related