ssut / nestjs-sqs

A project to make SQS easier to use within NestJS, with decorator-based handling and seamless NestJS-way integration.
MIT License
204 stars 54 forks source link

Possible code problem detected by linter #38

Open VinGarcia opened 1 year ago

VinGarcia commented 1 year ago

Hello, good morning.

I forked your repository for using at my company and I was requested to make some adaptations on it before using, and while on it the linter complained about one problem that actually seems important on the following lines of code:

https://github.com/ssut/nestjs-sqs/blob/master/lib/sqs.service.ts#L39-L53

     if (!metadata) {
        this.logger.warn(`No metadata found for: ${name}`);
      }

      const isBatchHandler = metadata.meta.batch === true;
      const consumer = Consumer.create({
        ...consumerOptions,
        ...(isBatchHandler
          ? {
              handleMessageBatch: metadata.discoveredMethod.handler.bind(
                metadata.discoveredMethod.parentClass.instance,
              ),
            }
          : { handleMessage: metadata.discoveredMethod.handler.bind(metadata.discoveredMethod.parentClass.instance) }),
      });

Here the code checks whether the metadata is present or not, but if it is undefined it will just continue running. And I don't think this is likely to work because we even make function calls on the subattributes like in:

metadata.discoveredMethod.handler.bind(
  metadata.discoveredMethod.parentClass.instance,
)

so even if I replaced all . with .? it would still break on that line.

I was wondering whether we might need to skip the iteration if we detect that the metadata is nil instead of just logging a warning.