UnauthorizedException Not Propagating from Dynamic Module's PermissionsUtil in NestJS

491 Views Asked by At

I'm encountering an issue with propagating an UnauthorizedException from a dynamic module's PermissionsUtil class. The exception is expected to be thrown during validation but ends up being caught as an InternalServerErrorException at the receiver end.

Here's the setup:

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 have been correctly configured and are working as expected.

Despite proper configuration, the UnauthorizedException raised within the dynamic module's PermissionsUtil class is not correctly propagating and is instead being caught as an InternalServerErrorException when used in the app module's controller. Any insights into why this exception is not propagating as intended would be greatly appreciated.

0

There are 0 best solutions below