Skip to content

Commit

Permalink
Fix SuppressedException
Browse files Browse the repository at this point in the history
Signed-off-by: shedaniel <daniel@shedaniel.me>
  • Loading branch information
shedaniel committed Aug 22, 2021
1 parent 62e58b5 commit ff42961
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ class CommandHandler(
try {
commandAcceptor.execute(event, ctx, args)
} catch (throwable: Throwable) {
try {
ctx.message.reply(ctx, {
dismissButton()
}) {
throwableHandler.generateThrowable(this, throwable, user)
if (throwableHandler.shouldError(throwable)) {
try {
ctx.message.reply(ctx, {
dismissButton()
}) {
throwableHandler.generateThrowable(this, throwable, user)
}
} catch (throwable2: Exception) {
throwable2.addSuppressed(throwable)
throwable2.printStackTrace()
}
} catch (throwable2: Exception) {
throwable2.addSuppressed(throwable)
throwable2.printStackTrace()
}
}
}
Expand All @@ -91,12 +93,14 @@ interface CommandAcceptor {
}

interface ThrowableHandler {
fun shouldError(throwable: Throwable): Boolean = true
fun generateErrorMessage(original: Message?, throwable: Throwable, channel: MessageChannel, user: User)
fun generateThrowable(builder: EmbedCreateSpec.Builder, throwable: Throwable, user: User)
}

open class SimpleThrowableHandler : ThrowableHandler {
override fun generateErrorMessage(original: Message?, throwable: Throwable, channel: MessageChannel, user: User) {
if (!shouldError(throwable)) return
try {
channel.sendEmbedMessage { generateThrowable(this, throwable, user) }.subscribe { message ->
buildReactions(Duration.ofMinutes(2)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,17 @@ class SlashCommands(
if (!executeOptions(command, ctx, optionsGetter, command.options, event.options) && !command.execute(command, ctx, optionsGetter)) {
}
}.exceptionOrNull()?.also { throwable ->
try {
ctx.message.reply(ctx, {
dismissButton()
}) {
throwableHandler.generateThrowable(this, throwable, ctx.user)
if (throwableHandler.shouldError(throwable)) {
try {
ctx.message.reply(ctx, {
dismissButton()
}) {
throwableHandler.generateThrowable(this, throwable, ctx.user)
}
} catch (throwable2: Exception) {
throwable2.addSuppressed(throwable)
throwable2.printStackTrace()
}
} catch (throwable2: Exception) {
throwable2.addSuppressed(throwable)
throwable2.printStackTrace()
}
}
if (!sentAny) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@

package me.shedaniel.linkie.discord

import discord4j.core.`object`.entity.Message
import discord4j.core.`object`.entity.User
import discord4j.core.`object`.entity.channel.MessageChannel
import discord4j.core.event.domain.message.MessageCreateEvent
import discord4j.core.spec.EmbedCreateSpec
import me.shedaniel.linkie.discord.handler.SimpleThrowableHandler
import me.shedaniel.linkie.discord.utils.CommandContext

object LinkieThrowableHandler : SimpleThrowableHandler() {
override fun generateErrorMessage(original: Message?, throwable: Throwable, channel: MessageChannel, user: User) {
if (throwable is SuppressedException) return
super.generateErrorMessage(original, throwable, channel, user)
}
override fun shouldError(throwable: Throwable): Boolean =
throwable !is SuppressedException && super.shouldError(throwable)

override fun generateThrowable(builder: EmbedCreateSpec.Builder, throwable: Throwable, user: User) {
super.generateThrowable(builder, throwable, user)
Expand Down

0 comments on commit ff42961

Please sign in to comment.