typescript:S1488 inside try catch statement

I have this code:

  async create(createdBy: string, createDynamicRoleDto: CreateDynamicRoleDto): Promise<DynamicRole> {
    const guild = await this.getGuild();
    const { channelId, name, shortDescription, color, emoji } = createDynamicRoleDto;
    let role: Role;
    let channel: TextChannel;
    try {
      role = await this.createRole(name, color, emoji);

      if (channelId) {
        channel = await this.updateChannel();
      } else {
        channel = await this.createChannel(name, shortDescription, role.id);
      }

      // TODO: Update dashboard

      const dynamicRole = await this.dynamicRolesRepository.create(
        createdBy,
        createDynamicRoleDto,
        channel.id,
        role.id
      );
      return dynamicRole;
    } catch (error) {
      if (role) {
        await guild.roles.delete(role.id);
      }

      if (channel && !channelId) {
        await guild.channels.delete(channel.id);
      }

      throw error;
    }
  }

It’s pretty simple. Create/update channel create a role and a new db entry. if anything breaks, check if the role and channel exist and delete them (rollback).

The thing is, i haveto return the dynamic role, after the call has finished. Else if the db entry fails, the catch wont be executed.

I get the rule and wouldn’t want to disable it, but it’s bad in this case.
Maybe the rule should be disabled automatically inside a try catch

Thanks for your feedback. We have recently reviewed the rule and we think that it should not be part of the default profile. A temporary variable is often used to give a name to the result, and it’s hard to design the rule which would consider this. You can keep using the rule by creating your custom profile.

1 Like