Skip to content

Commit c71ec52

Browse files
committed
refactor(application-form): better exception handling
There are currently many undesired conditions the code could run into when it comes to the `RoleApplicationHandler` which we would not desire and we would not be prepared for, particularly in situations where: - We would somehow find ourselves handling application form submissions in a non-guild environment. - The arguments size including the clicked component ID as well as the role name (the title of that component ID in essence) would have an unexpected size. - The staff-monitored application submissions text channel would not be found by the bot. We do not have entire control over these cases, but we need to handle them properly and set ourselves up for success in the rare case they happen. Make `sendApplicationResult` throw an `IllegalArgumentException` with descriptive messages if anything goes wrong, and log appropriate stacktraces if anything goes wrong in the logger for easy debugging. I thought about printing straight to the logger and not using any `IllegalArgumentException` in the code (or at least, using a different or maybe custom `Exception` but that would be too much), but with this method, we get to see custom messages along with exceptions for each individual case - all that without unnecessarily overcomplicating the code and sacrificing its readability to a considerable extent. Suggested-by: Zabuzard <[email protected]> Signed-off-by: Chris Sdogkos <[email protected]>
1 parent 1507cc1 commit c71ec52

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

application/src/main/java/org/togetherjava/tjbot/features/roleapplication/RoleApplicationHandler.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
1111
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
1212
import net.dv8tion.jda.api.interactions.modals.ModalMapping;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
1315

1416
import org.togetherjava.tjbot.config.RoleApplicationSystemConfig;
1517

@@ -29,8 +31,9 @@
2931
* users to prevent spamming.
3032
*/
3133
public class RoleApplicationHandler {
32-
private static final int APPLICATION_SUBMIT_COOLDOWN_MINUTES = 5;
34+
private static final Logger logger = LoggerFactory.getLogger(RoleApplicationHandler.class);
3335

36+
private static final int APPLICATION_SUBMIT_COOLDOWN_MINUTES = 5;
3437
private final Cache<Member, OffsetDateTime> applicationSubmitCooldown;
3538
private final Predicate<String> applicationChannelPattern;
3639
private final RoleApplicationSystemConfig roleApplicationSystemConfig;
@@ -65,17 +68,26 @@ public RoleApplicationHandler(RoleApplicationSystemConfig roleApplicationSystemC
6568
* @param answer the answer provided by the applicant to the default question
6669
*/
6770
protected void sendApplicationResult(final ModalInteractionEvent event, List<String> args,
68-
String answer) {
71+
String answer) throws IllegalArgumentException {
6972
Guild guild = event.getGuild();
70-
if (args.size() != 2 || guild == null) {
71-
return;
73+
74+
if (guild == null) {
75+
throw new IllegalArgumentException(
76+
"sendApplicationResult() got fired in a non-guild environment.");
77+
}
78+
79+
if (args.size() != 2) {
80+
throw new IllegalArgumentException(
81+
"Received application result after user submitted one, and did not receive 2 arguments. Args: "
82+
+ args);
7283
}
7384

7485
String roleString = args.get(1);
7586

7687
Optional<TextChannel> applicationChannel = getApplicationChannel(guild);
7788
if (applicationChannel.isEmpty()) {
78-
return;
89+
throw new IllegalArgumentException("Application channel %s could not be found."
90+
.formatted(roleApplicationSystemConfig.submissionsChannelPattern()));
7991
}
8092

8193
User applicant = event.getUser();
@@ -125,10 +137,17 @@ protected void submitApplicationFromModalInteraction(ModalInteractionEvent event
125137

126138
ModalMapping modalAnswer = event.getValues().getFirst();
127139

128-
sendApplicationResult(event, args, modalAnswer.getAsString());
129-
event.reply("Your application has been submitted. Thank you for applying! 😎")
130-
.setEphemeral(true)
131-
.queue();
140+
try {
141+
sendApplicationResult(event, args, modalAnswer.getAsString());
142+
event.reply("Your application has been submitted. Thank you for applying! 😎")
143+
.setEphemeral(true)
144+
.queue();
145+
} catch (IllegalArgumentException e) {
146+
logger.error("A role application could not be submitted. ", e);
147+
event.reply("Your application could not be submitted. Please contact the staff team.")
148+
.setEphemeral(true)
149+
.queue();
150+
}
132151

133152
applicationSubmitCooldown.put(event.getMember(), OffsetDateTime.now());
134153
}

0 commit comments

Comments
 (0)