Skip to content

Commit

Permalink
Clean up exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andyksaw committed Nov 24, 2024
1 parent a2cd594 commit 8f0012d
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 129 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,10 @@ internal interface PCBRequest {
): Build

@DELETE("v2/minecraft/build/{id}")
@FormUrlEncoded
suspend fun deleteBuild(
@Path(value = "id") id: Int,
@Field(value = "player_uuid") playerUUID: String,
): Build
@Query(value = "player_uuid") playerUUID: String,
)

@POST("v2/minecraft/build/{id}/vote")
@FormUrlEncoded
Expand All @@ -157,9 +156,8 @@ internal interface PCBRequest {
): Build

@DELETE("v2/minecraft/build/{id}/vote")
@FormUrlEncoded
suspend fun buildUnvote(
@Path(value = "id") id: Int,
@Field(value = "player_uuid") playerUUID: String,
@Query(value = "player_uuid") playerUUID: String,
): Build
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRep
import com.projectcitybuild.pcbridge.paper.support.brigadier.BrigadierCommand
import com.projectcitybuild.pcbridge.paper.support.brigadier.executesSuspending
import com.projectcitybuild.pcbridge.paper.support.brigadier.suggestsSuspending
import com.projectcitybuild.pcbridge.paper.support.brigadier.traceCommand
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import kotlinx.coroutines.future.await
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.title.Title
import org.bukkit.Location
import org.bukkit.Server
import org.bukkit.event.player.PlayerTeleportEvent
Expand Down Expand Up @@ -44,23 +48,14 @@ class BuildCommand(
.forEach(suggestions::suggest)
}

private suspend fun execute(context: CommandContext<CommandSourceStack>) {
private suspend fun execute(context: CommandContext<CommandSourceStack>) = traceCommand(context) {
val name = context.getArgument("name", String::class.java)

val build = buildRepository.get(name = name)
if (build == null) {
context.source.sender.sendMessage(
MiniMessage.miniMessage().deserialize("Build not found")
)
return
}
checkNotNull(build) { "Build not found" }

val world = server.getWorld(build.world)
if (world == null) {
context.source.sender.sendMessage(
MiniMessage.miniMessage().deserialize("<red>Error: Could not find world ${build.world}</red>")
)
return
}
checkNotNull(world) { "Could not find world {$build.world}" }

context.source.sender.sendMessage(
MiniMessage.miniMessage().deserialize("<gray>Teleporting to $name...</gray>")
Expand All @@ -73,9 +68,18 @@ class BuildCommand(
build.yaw,
build.pitch,
)
context.source.executor?.teleportAsync(

val didTeleport = context.source.executor?.teleportAsync(
location,
PlayerTeleportEvent.TeleportCause.COMMAND,
)
)?.await()

if (didTeleport == true) {
val title = Title.title(
Component.text(build.name),
Component.text("TODO"),
)
context.source.executor?.showTitle(title)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.projectcitybuild.pcbridge.paper.features.builds.commands.builds
import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.tree.LiteralCommandNode
import com.projectcitybuild.pcbridge.http.parsing.ResponseParser
import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository
import com.projectcitybuild.pcbridge.paper.support.brigadier.BrigadierCommand
import com.projectcitybuild.pcbridge.paper.support.brigadier.executesSuspending
import com.projectcitybuild.pcbridge.paper.support.brigadier.traceCommand
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import net.kyori.adventure.text.minimessage.MiniMessage
Expand All @@ -27,40 +27,22 @@ class BuildCreateCommand(
.build()
}

private suspend fun execute(context: CommandContext<CommandSourceStack>) {
private suspend fun execute(context: CommandContext<CommandSourceStack>) = traceCommand(context) {
val name = context.getArgument("name", String::class.java)
val player = context.source.executor as? Player

val miniMessage = MiniMessage.miniMessage()
if (player == null) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>Only a player can use this command</red>")
)
return
}
val location = player.location
checkNotNull(player) { "Only a player can use this command" }

val build = try {
buildRepository.create(
name = name,
player = player,
world = location.world.name,
location = player.location,
)
} catch (error: ResponseParser.ValidationError) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>Error: ${error.message}</red>")
)
return
} catch (error: Exception) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>An unexpected error occurred</red>")
)
throw error
}
val location = player.location
val build = buildRepository.create(
name = name,
player = player,
world = location.world.name,
location = player.location,
)

context.source.sender.sendMessage(
miniMessage.deserialize("<green>${build.name} created</green>")
MiniMessage.miniMessage().deserialize("<green>${build.name} created</green>")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import com.mojang.brigadier.tree.LiteralCommandNode
import com.projectcitybuild.pcbridge.http.parsing.ResponseParser
import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository
import com.projectcitybuild.pcbridge.paper.support.brigadier.BrigadierCommand
import com.projectcitybuild.pcbridge.paper.support.brigadier.executesSuspending
import com.projectcitybuild.pcbridge.paper.support.brigadier.suggestsSuspending
import com.projectcitybuild.pcbridge.paper.support.brigadier.traceCommand
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import net.kyori.adventure.text.minimessage.MiniMessage
Expand Down Expand Up @@ -42,37 +42,19 @@ class BuildDeleteCommand(
.forEach(suggestions::suggest)
}

private suspend fun execute(context: CommandContext<CommandSourceStack>) {
private suspend fun execute(context: CommandContext<CommandSourceStack>) = traceCommand(context) {
val name = context.getArgument("name", String::class.java)
val player = context.source.executor as? Player

val miniMessage = MiniMessage.miniMessage()
if (player == null) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>Only a player can use this command</red>")
)
return
}
checkNotNull(player) { "Only a player can use this command" }

try {
buildRepository.delete(
name = name,
player = player,
)
} catch (error: ResponseParser.ValidationError) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>Error: ${error.message}</red>")
)
return
} catch (error: Exception) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>An unexpected error occurred</red>")
)
throw error
}
buildRepository.delete(
name = name,
player = player,
)

context.source.sender.sendMessage(
miniMessage.deserialize("<green>${name} deleted</green>")
MiniMessage.miniMessage().deserialize("<green>${name} deleted</green>")
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.projectcitybuild.pcbridge.paper.features.builds.commands.builds

import com.github.shynixn.mccoroutine.bukkit.launch
import com.mojang.brigadier.arguments.IntegerArgumentType
import com.mojang.brigadier.tree.LiteralCommandNode
import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository
Expand Down Expand Up @@ -53,7 +52,7 @@ class BuildListCommand(
)

builds.data.forEach { build ->
val text = "<gray>[<white>${build.id}</white>] \"<aqua>${build.name}</aqua>\" (${build.votes} votes)</gray>"
val text = "<gray>#${build.id} \"<aqua>${build.name}</aqua>\" (<white>${build.votes}</white> votes)</gray>"

sender.sendMessage(
miniMessage.deserialize(text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import com.mojang.brigadier.tree.LiteralCommandNode
import com.projectcitybuild.pcbridge.http.parsing.ResponseParser
import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository
import com.projectcitybuild.pcbridge.paper.support.brigadier.BrigadierCommand
import com.projectcitybuild.pcbridge.paper.support.brigadier.executesSuspending
import com.projectcitybuild.pcbridge.paper.support.brigadier.suggestsSuspending
import com.projectcitybuild.pcbridge.paper.support.brigadier.traceCommand
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import net.kyori.adventure.text.minimessage.MiniMessage
Expand Down Expand Up @@ -42,40 +42,22 @@ class BuildMoveCommand(
.forEach(suggestions::suggest)
}

private suspend fun execute(context: CommandContext<CommandSourceStack>) {
private suspend fun execute(context: CommandContext<CommandSourceStack>) = traceCommand(context) {
val name = context.getArgument("name", String::class.java)
val player = context.source.executor as? Player

val miniMessage = MiniMessage.miniMessage()
if (player == null) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>Only a player can use this command</red>")
)
return
}
val location = player.location
checkNotNull(player) { "Only a player can use this command" }

val build = try {
buildRepository.update(
name = name,
player = player,
world = location.world.name,
location = player.location,
)
} catch (error: ResponseParser.ValidationError) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>Error: ${error.message}</red>")
)
return
} catch (error: Exception) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>An unexpected error occurred</red>")
)
throw error
}
val location = player.location
val build = buildRepository.update(
name = name,
player = player,
world = location.world.name,
location = player.location,
)

context.source.sender.sendMessage(
miniMessage.deserialize("<green>${build.name} moved to your location</green>")
MiniMessage.miniMessage().deserialize("<green>${build.name} moved to your location</green>")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import com.mojang.brigadier.tree.LiteralCommandNode
import com.projectcitybuild.pcbridge.http.parsing.ResponseParser
import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository
import com.projectcitybuild.pcbridge.paper.support.brigadier.BrigadierCommand
import com.projectcitybuild.pcbridge.paper.support.brigadier.executesSuspending
import com.projectcitybuild.pcbridge.paper.support.brigadier.suggestsSuspending
import com.projectcitybuild.pcbridge.paper.support.brigadier.traceCommand
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import net.kyori.adventure.text.minimessage.MiniMessage
Expand Down Expand Up @@ -42,34 +42,16 @@ class BuildVoteCommand(
.forEach(suggestions::suggest)
}

private suspend fun execute(context: CommandContext<CommandSourceStack>) {
private suspend fun execute(context: CommandContext<CommandSourceStack>) = traceCommand(context) {
val name = context.getArgument("name", String::class.java)
val player = context.source.executor as? Player

val miniMessage = MiniMessage.miniMessage()
if (player == null) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>Only a player can use this command</red>")
)
return
}
checkNotNull(player) { "Only a player can use this command</red>" }

val build = try {
buildRepository.vote(name = name, player = player)
} catch (error: ResponseParser.ValidationError) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>Error: ${error.message}</red>")
)
return
} catch (error: Exception) {
context.source.sender.sendMessage(
miniMessage.deserialize("<red>An unexpected error occurred</red>")
)
throw error
}
val build = buildRepository.vote(name = name, player = player)

context.source.sender.sendMessage(
miniMessage.deserialize("<green>You voted for ${build.name}</green>")
MiniMessage.miniMessage().deserialize("<green>You voted for ${build.name}</green>")
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.projectcitybuild.pcbridge.paper.support.brigadier

import com.mojang.brigadier.context.CommandContext
import com.projectcitybuild.pcbridge.http.parsing.ResponseParser
import com.projectcitybuild.pcbridge.paper.support.spigot.BadCommandUsageException
import com.projectcitybuild.pcbridge.paper.support.spigot.UnauthorizedCommandException
import io.papermc.paper.command.brigadier.CommandSourceStack
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.command.CommandSender

@Suppress("UnstableApiUsage")
suspend fun traceCommand(
context: CommandContext<CommandSourceStack>,
block: suspend (CommandContext<CommandSourceStack>) -> Unit,
) {
try {
block(context)
} catch (e: IllegalStateException) {
context.source.sender.sendError("Error: ${e.message}")
} catch (e: BadCommandUsageException) {
// TODO
} catch (e: UnauthorizedCommandException) {
// TODO: is this still needed?
context.source.sender.sendError("Error: You do not have permission to use this command")
} catch (e: ResponseParser.ValidationError) {
context.source.sender.sendError("Error: {${e.message}")
} catch (e: Exception) {
context.source.sender.sendError("An unexpected error occurred")

// Rethrow so that it bubbles up to the error reporter
throw e
}
}

private fun CommandSender.sendError(message: String) {
sendMessage(
MiniMessage.miniMessage().deserialize("<red>$message</red>")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextDecoration

@Deprecated("Use Brigadier instead")
class CommandHelpBuilder(
private val usage: String?,
) {
Expand Down
Loading

0 comments on commit 8f0012d

Please sign in to comment.