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