Skip to content

Commit

Permalink
Refactor command/parse errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Dec 20, 2019
1 parent 47bfe83 commit 822e817
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/me/devoxin/flight/FlightInfo.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package me.devoxin.flight

object FlightInfo {
val VERSION = "1.2.2"
val VERSION = "1.3.0"
}
10 changes: 5 additions & 5 deletions src/main/kotlin/me/devoxin/flight/api/CommandClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ class CommandClient(
try {
arguments = ArgParser.parseArguments(cmd, ctx, args)
} catch (e: BadArgument) {
return eventListeners.forEach { it.onBadArgument(ctx, e) }
return eventListeners.forEach { it.onBadArgument(ctx, cmd, e) }
} catch (e: Throwable) {
return eventListeners.forEach { it.onParseError(ctx, e) }
return eventListeners.forEach { it.onParseError(ctx, cmd, e) }
}

val cb = { success: Boolean, err: CommandError? ->
val cb = { success: Boolean, err: Throwable? ->
if (err != null) {
val handled = err.command.cog.onCommandError(ctx, err)
val handled = cmd.cog.onCommandError(ctx, cmd, err)

if (!handled) {
eventListeners.forEach { it.onCommandError(ctx, err) }
eventListeners.forEach { it.onCommandError(ctx, cmd, err) }
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/main/kotlin/me/devoxin/flight/api/CommandError.kt

This file was deleted.

10 changes: 4 additions & 6 deletions src/main/kotlin/me/devoxin/flight/api/CommandWrapper.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package me.devoxin.flight.api

import me.devoxin.flight.annotations.Command
import me.devoxin.flight.api.CommandError
import me.devoxin.flight.api.Context
import me.devoxin.flight.arguments.Argument
import me.devoxin.flight.models.Cog
import java.lang.reflect.Method
Expand All @@ -21,25 +19,25 @@ class CommandWrapper(
/**
* Calls the related method with the given args.
*/
fun execute(ctx: Context, vararg additionalArgs: Any?, complete: (Boolean, CommandError?) -> Unit) {
fun execute(ctx: Context, vararg additionalArgs: Any?, complete: (Boolean, Throwable?) -> Unit) {
try {
method.invoke(cog, ctx, *additionalArgs)
complete(true, null)
} catch (e: Throwable) {
complete(false, CommandError(e.cause ?: e, this))
complete(false, e.cause ?: e)
}
}

/**
* Calls the related method with the given args, except in an async manner.
*/
suspend fun executeAsync(ctx: Context, vararg additionalArgs: Any?, complete: (Boolean, CommandError?) -> Unit) {
suspend fun executeAsync(ctx: Context, vararg additionalArgs: Any?, complete: (Boolean, Throwable?) -> Unit) {
suspendCoroutine<Unit> {
try {
method.invoke(cog, ctx, *additionalArgs, it)
complete(true, null)
} catch (e: Throwable) {
complete(false, CommandError(e.cause ?: e, this))
complete(false, e.cause ?: e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import net.dv8tion.jda.api.Permission

abstract class DefaultCommandClientAdapter : CommandClientAdapter {

override fun onBadArgument(ctx: Context, error: BadArgument) {}
override fun onBadArgument(ctx: Context, command: CommandWrapper, error: BadArgument) {}

override fun onCommandError(ctx: Context, error: CommandError) {}
override fun onCommandError(ctx: Context, command: CommandWrapper, error: Throwable) {}

override fun onCommandPostInvoke(ctx: Context, command: CommandWrapper, failed: Boolean) {}

override fun onCommandPreInvoke(ctx: Context, command: CommandWrapper) = true

override fun onParseError(ctx: Context, error: Throwable) {}
override fun onParseError(ctx: Context, command: CommandWrapper, error: Throwable) {}

override fun onBotMissingPermissions(ctx: Context, command: CommandWrapper, permissions: List<Permission>) {}

Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/me/devoxin/flight/exceptions/BadArgument.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package me.devoxin.flight.exceptions
import me.devoxin.flight.arguments.Argument

class BadArgument(
val argument: Argument,
val providedArgument: String,
val original: Throwable? = null
val argument: Argument,
val providedArgument: String,
val original: Throwable? = null
) : Throwable("`${argument.name}` must be a `${argument.type.simpleName}`")
3 changes: 1 addition & 2 deletions src/main/kotlin/me/devoxin/flight/models/Cog.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.devoxin.flight.models

import me.devoxin.flight.api.CommandError
import me.devoxin.flight.api.CommandWrapper
import me.devoxin.flight.api.Context

Expand All @@ -14,7 +13,7 @@ interface Cog {
* the error will be passed back to the registered
* CommandClientAdapter for handling.
*/
fun onCommandError(ctx: Context, error: CommandError): Boolean = false
fun onCommandError(ctx: Context, command: CommandWrapper, error: Throwable): Boolean = false

/**
* Invoked before a command is executed. This check is local to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.devoxin.flight.models

import me.devoxin.flight.exceptions.BadArgument
import me.devoxin.flight.api.CommandError
import me.devoxin.flight.api.CommandWrapper
import me.devoxin.flight.api.Context
import net.dv8tion.jda.api.Permission
Expand All @@ -11,12 +10,12 @@ interface CommandClientAdapter {
/**
* Invoked when an invalid argument is passed.
*/
fun onBadArgument(ctx: Context, error: BadArgument)
fun onBadArgument(ctx: Context, command: CommandWrapper, error: BadArgument)

/**
* Invoked when the parser encounters an internal error.
*/
fun onParseError(ctx: Context, error: Throwable)
fun onParseError(ctx: Context, command: CommandWrapper, error: Throwable)

/**
* Invoked before a command is executed. Useful for logging command usage etc.
Expand All @@ -40,7 +39,7 @@ interface CommandClientAdapter {
/**
* Invoked when a command encounters an error during execution.
*/
fun onCommandError(ctx: Context, error: CommandError)
fun onCommandError(ctx: Context, command: CommandWrapper, error: Throwable)

/**
* Invoked when a user lacks permissions to execute a command
Expand Down

0 comments on commit 822e817

Please sign in to comment.