Skip to content

Commit

Permalink
Add InvalidUsageException, Refactor exception handling. Release 3.0.0…
Browse files Browse the repository at this point in the history
…-BETA-pre20
  • Loading branch information
Rollczi committed Oct 5, 2023
1 parent b9e55e6 commit b9b5982
Show file tree
Hide file tree
Showing 32 changed files with 174 additions and 130 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Framework Core
<dependency>
<groupId>dev.rollczi</groupId>
<artifactId>litecommands-core</artifactId>
<version>3.0.0-BETA-pre19</version>
<version>3.0.0-BETA-pre20</version>
</dependency>
```

```groovy
implementation 'dev.rollczi:litecommands-core:3.0.0-BETA-pre19'
implementation 'dev.rollczi:litecommands-core:3.0.0-BETA-pre20'
```

### First Simple Command
Expand Down Expand Up @@ -85,12 +85,12 @@ Add this to your dependencies if you want to use ready-made implementation for v
<dependency>
<groupId>dev.rollczi</groupId>
<artifactId>litecommands-velocity</artifactId>
<version>3.0.0-BETA-pre19</version>
<version>3.0.0-BETA-pre20</version>
</dependency>
```

```groovy
implementation 'dev.rollczi:litecommands-velocity:3.0.0-BETA-pre19'
implementation 'dev.rollczi:litecommands-velocity:3.0.0-BETA-pre20'
```

#### Add -parameters to your compiler to use all features of LiteCommands
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/litecommands-publish.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "dev.rollczi"
version = "3.0.0-BETA-pre19"
version = "3.0.0-BETA-pre20"

java {
withSourcesJar()
Expand Down
4 changes: 2 additions & 2 deletions examples/bukkit-adventure-platform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ repositories {
dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")

// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre19") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-adventure-platform:3.0.0-BETA-pre19") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre20") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-adventure-platform:3.0.0-BETA-pre20") // <-- uncomment in your project
implementation("net.kyori:adventure-platform-bukkit:4.3.0")
implementation("net.kyori:adventure-text-minimessage:4.14.0")

Expand Down
4 changes: 2 additions & 2 deletions examples/bukkit-chatgpt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ repositories {
dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")

// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre19") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-chatgpt:3.0.0-BETA-pre19") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre20") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-chatgpt:3.0.0-BETA-pre20") // <-- uncomment in your project
implementation(project(":litecommands-bukkit")) // don't use this line in your build.gradle
implementation(project(":litecommands-chatgpt")) // don't use this line in your build.gradle
}
Expand Down
2 changes: 1 addition & 1 deletion examples/bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repositories {
dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")

// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre19") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre20") // <-- uncomment in your project
implementation(project(":litecommands-bukkit")) // don't use this line in your build.gradle
}

Expand Down
2 changes: 1 addition & 1 deletion examples/velocity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies {
compileOnly("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT")
annotationProcessor("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT")

// implementation("dev.rollczi:litecommands-velocity:3.0.0-BETA-pre19") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-velocity:3.0.0-BETA-pre20") // <-- uncomment in your project
implementation(project(":litecommands-velocity")) // don't use this line in your build.gradle
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void beforeAll(TestInfo testInfo) {
liteCommands = LiteCommandsFactory.builder(TestSender.class, platform)
.commands(LiteCommandsAnnotations.ofClasses(commands))
.preProcessor((builder, pattern) -> configureLiteTest(builder, type))
.exceptionUnexpected((invocation, exception) -> {})
.exceptionUnexpected((invocation, exception, chain) -> {})
.build(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ExceptionHandleTest extends LiteTestSpec {
private static final Set<String> exceptions = new HashSet<>();

static LiteConfig config = builder -> builder
.exception(IllegalArgumentException.class, (invocation, exception) -> exceptions.add("IllegalArgumentException"))
.exception(RuntimeException.class, (invocation, exception) -> exceptions.add("RuntimeException"));
.exception(IllegalArgumentException.class, (invocation, exception, chain) -> exceptions.add("IllegalArgumentException"))
.exception(RuntimeException.class, (invocation, exception, chain) -> exceptions.add("RuntimeException"));

@Command(name = "test")
static class TestCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MissingPermissionResultHandlerTest extends LiteTestSpec {
missingPermissionHandler.set(true);
})
.invalidUsage((invocation, command, chain) -> invalidHandler.set(true))
.exception(RuntimeException.class, (invocation, exception) -> throwHandled.set(true));
.exception(RuntimeException.class, (invocation, exception, chain) -> throwHandled.set(true));

@Command(name = "test sub")
@Permission("test.permission")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import dev.rollczi.litecommands.builder.LiteCommandsBuilder;
import dev.rollczi.litecommands.context.ContextResult;
import dev.rollczi.litecommands.flag.FlagArgument;
import dev.rollczi.litecommands.handler.result.basic.CompletionStageHandler;
import dev.rollczi.litecommands.handler.result.basic.OptionHandler;
import dev.rollczi.litecommands.handler.result.basic.OptionalHandler;
import dev.rollczi.litecommands.handler.result.basic.ThrowableHandler;
import dev.rollczi.litecommands.handler.exception.standard.InvocationTargetExceptionHandler;
import dev.rollczi.litecommands.handler.exception.standard.LiteCommandsExceptionHandler;
import dev.rollczi.litecommands.handler.result.standard.CompletionStageHandler;
import dev.rollczi.litecommands.handler.result.standard.OptionHandler;
import dev.rollczi.litecommands.handler.result.standard.OptionalHandler;
import dev.rollczi.litecommands.handler.exception.standard.ThrowableHandler;
import dev.rollczi.litecommands.invalidusage.InvalidUsage;
import dev.rollczi.litecommands.invalidusage.InvalidUsageException;
import dev.rollczi.litecommands.invalidusage.InvalidUsageExceptionHandler;
import dev.rollczi.litecommands.invalidusage.InvalidUsageHandlerImpl;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.join.JoinArgument;
Expand All @@ -29,6 +33,7 @@
import dev.rollczi.litecommands.wrapper.std.OptionalWrapper;
import panda.std.Option;

import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import java.util.concurrent.CompletionStage;

Expand Down Expand Up @@ -71,7 +76,11 @@ public static <SENDER, C extends PlatformSettings, B extends LiteCommandsBaseBui
.wrapper(new OptionalWrapper())
.wrapper(new CompletableFutureWrapper(scheduler))

.result(Throwable.class, new ThrowableHandler<>())
.exception(Throwable.class, new ThrowableHandler<>())
.exception(InvalidUsageException.class, new InvalidUsageExceptionHandler<>())
.exception(InvocationTargetException.class, new InvocationTargetExceptionHandler<>())
.exception(LiteCommandsException.class, new LiteCommandsExceptionHandler<>())

.result(Optional.class, new OptionalHandler<>())
.result(Option.class, new OptionHandler<>())
.result(CompletionStage.class, new CompletionStageHandler<>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import dev.rollczi.litecommands.builder.extension.LiteExtension;
import dev.rollczi.litecommands.context.ContextRegistry;
import dev.rollczi.litecommands.editor.Editor;
import dev.rollczi.litecommands.handler.exception.ExceptionHandleService;
import dev.rollczi.litecommands.handler.exception.ExceptionHandler;
import dev.rollczi.litecommands.handler.result.ResultHandleService;
import dev.rollczi.litecommands.handler.result.ResultHandleServiceImpl;
Expand Down Expand Up @@ -83,7 +82,6 @@ public class LiteCommandsBaseBuilder<SENDER, C extends PlatformSettings, B exten
protected final BindRegistry bindRegistry = new BindRegistry();
protected final ContextRegistry<SENDER> contextRegistry = new ContextRegistry<>();
protected final ResultHandleService<SENDER> resultHandleService = new ResultHandleServiceImpl<>();
protected final ExceptionHandleService<SENDER> exceptionHandleService = new ExceptionHandleService<>();
protected final CommandBuilderCollector<SENDER> commandBuilderCollector = new CommandBuilderCollector<>();
protected final MessageRegistry<SENDER> messageRegistry = new MessageRegistry<SENDER>();
protected final WrapperRegistry wrapperRegistry = new WrapperRegistry();
Expand Down Expand Up @@ -319,13 +317,13 @@ public LiteCommandsBuilder<SENDER, C, B> resultUnexpected(ResultHandler<SENDER,

@Override
public <E extends Throwable> LiteCommandsBuilder<SENDER, C, B> exception(Class<E> exceptionType, ExceptionHandler<SENDER, ? extends E> handler) {
this.exceptionHandleService.registerHandler(exceptionType, handler);
this.resultHandleService.registerHandler(exceptionType, handler);
return this;
}

@Override
public LiteCommandsBuilder<SENDER, C, B> exceptionUnexpected(ExceptionHandler<SENDER, Throwable> handler) {
this.exceptionHandleService.registerUnexpectedHandler(handler);
this.resultHandleService.registerHandler(Throwable.class, handler);
return this;
}

Expand Down Expand Up @@ -411,7 +409,7 @@ public LiteCommands<SENDER> build(boolean register) {
processor.process(this, this);
}

CommandExecuteService<SENDER> commandExecuteService = new CommandExecuteService<>(validatorService, resultHandleService, exceptionHandleService, scheduler, schematicGenerator, parserRegistry, contextRegistry, wrapperRegistry, bindRegistry);
CommandExecuteService<SENDER> commandExecuteService = new CommandExecuteService<>(validatorService, resultHandleService, scheduler, schematicGenerator, parserRegistry, contextRegistry, wrapperRegistry, bindRegistry);
SuggestionService<SENDER> suggestionService = new SuggestionService<>(parserRegistry, suggesterRegistry, validatorService);
CommandManager<SENDER> commandManager = new CommandManager<>(this.platform, commandExecuteService, suggestionService);

Expand Down Expand Up @@ -510,12 +508,6 @@ public CommandBuilderCollector<SENDER> getCommandBuilderCollector() {
return this.commandBuilderCollector;
}

@Override
@ApiStatus.Internal
public ExceptionHandleService<SENDER> getExceptionHandleService() {
return this.exceptionHandleService;
}

@Override
@ApiStatus.Internal
public MessageRegistry<SENDER> getMessageRegistry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dev.rollczi.litecommands.argument.parser.ParserRegistry;
import dev.rollczi.litecommands.bind.BindRegistry;
import dev.rollczi.litecommands.context.ContextRegistry;
import dev.rollczi.litecommands.handler.exception.ExceptionHandleService;
import dev.rollczi.litecommands.handler.result.ResultHandleService;
import dev.rollczi.litecommands.command.builder.CommandBuilderCollector;
import dev.rollczi.litecommands.editor.EditorService;
Expand Down Expand Up @@ -55,9 +54,6 @@ public interface LiteCommandsInternalBuilderApi<SENDER, C extends PlatformSettin
@ApiStatus.Internal
CommandBuilderCollector<SENDER> getCommandBuilderCollector();

@ApiStatus.Internal
ExceptionHandleService<SENDER> getExceptionHandleService();

@ApiStatus.Internal
MessageRegistry<SENDER> getMessageRegistry();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import dev.rollczi.litecommands.requirement.ContextRequirement;
import dev.rollczi.litecommands.requirement.Requirement;
import dev.rollczi.litecommands.requirement.RequirementsResult;
import dev.rollczi.litecommands.LiteCommandsException;
import dev.rollczi.litecommands.handler.result.ResultHandleService;
import dev.rollczi.litecommands.invalidusage.InvalidUsage.Cause;
import dev.rollczi.litecommands.scheduler.ScheduledChainException;
Expand All @@ -23,7 +22,6 @@
import dev.rollczi.litecommands.shared.FailedReason;
import dev.rollczi.litecommands.requirement.RequirementResult;
import dev.rollczi.litecommands.requirement.RequirementMatch;
import dev.rollczi.litecommands.handler.exception.ExceptionHandleService;
import dev.rollczi.litecommands.flow.Flow;
import dev.rollczi.litecommands.invalidusage.InvalidUsage;
import dev.rollczi.litecommands.invocation.Invocation;
Expand Down Expand Up @@ -51,18 +49,16 @@ public class CommandExecuteService<SENDER> {

private final ValidatorService<SENDER> validatorService;
private final ResultHandleService<SENDER> resultResolver;
private final ExceptionHandleService<SENDER> exceptionHandleService;
private final Scheduler scheduler;
private final SchematicGenerator<SENDER> schematicGenerator;
private final ParserRegistry<SENDER> parserRegistry;
private final ContextRegistry<SENDER> contextRegistry;
private final WrapperRegistry wrapperRegistry;
private final BindRegistry bindRegistry;

public CommandExecuteService(ValidatorService<SENDER> validatorService, ResultHandleService<SENDER> resultResolver, ExceptionHandleService<SENDER> exceptionHandleService, Scheduler scheduler, SchematicGenerator<SENDER> schematicGenerator, ParserRegistry<SENDER> parserRegistry, ContextRegistry<SENDER> contextRegistry, WrapperRegistry wrapperRegistry, BindRegistry bindRegistry) {
public CommandExecuteService(ValidatorService<SENDER> validatorService, ResultHandleService<SENDER> resultResolver, Scheduler scheduler, SchematicGenerator<SENDER> schematicGenerator, ParserRegistry<SENDER> parserRegistry, ContextRegistry<SENDER> contextRegistry, WrapperRegistry wrapperRegistry, BindRegistry bindRegistry) {
this.validatorService = validatorService;
this.resultResolver = resultResolver;
this.exceptionHandleService = exceptionHandleService;
this.scheduler = scheduler;
this.schematicGenerator = schematicGenerator;
this.parserRegistry = parserRegistry;
Expand All @@ -79,13 +75,13 @@ public CompletableFuture<CommandExecuteResult> execute(Invocation<SENDER> invoca

return executeResult;
}))
.exceptionally(new LastExceptionHandler<>(exceptionHandleService, invocation));
.exceptionally(new LastExceptionHandler<>(resultResolver, invocation));
}

private void handleResult(Invocation<SENDER> invocation, CommandExecuteResult executeResult) {
Throwable throwable = executeResult.getThrowable();
if (throwable != null) {
exceptionHandleService.resolve(invocation, throwable);
resultResolver.resolve(invocation, throwable);
}

Object result = executeResult.getResult();
Expand Down Expand Up @@ -190,9 +186,8 @@ private <MATCHER extends ParseableInputMatcher<MATCHER>> CompletableFuture<Comma
return scheduler.supply(type, () -> {
try {
return match.executeCommand();
} catch (LiteCommandsException exception) {
return CommandExecuteResult.thrown(executor, exception.getCause());
} catch (Throwable error) {
}
catch (Throwable error) {
return CommandExecuteResult.thrown(executor, error);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package dev.rollczi.litecommands.command.executor;

import dev.rollczi.litecommands.handler.exception.ExceptionHandleService;
import dev.rollczi.litecommands.handler.result.ResultHandleService;
import dev.rollczi.litecommands.invocation.Invocation;

import java.util.concurrent.CompletionException;
import java.util.function.Function;

public class LastExceptionHandler<SENDER> implements Function<Throwable, CommandExecuteResult> {

private final ExceptionHandleService<SENDER> exceptionHandleService;
private final ResultHandleService<SENDER> resultHandleService;
private final Invocation<SENDER> invocation;

public LastExceptionHandler(ExceptionHandleService<SENDER> exceptionHandleService, Invocation<SENDER> invocation) {
this.exceptionHandleService = exceptionHandleService;
public LastExceptionHandler(ResultHandleService<SENDER> resultHandleService, Invocation<SENDER> invocation) {
this.resultHandleService = resultHandleService;
this.invocation = invocation;
}

Expand All @@ -23,7 +23,7 @@ public CommandExecuteResult apply(Throwable throwable) {
}

try {
this.exceptionHandleService.resolve(invocation, throwable);
this.resultHandleService.resolve(invocation, throwable);
}
catch (Throwable lastError) { // Handle exception from exception handler
lastError.printStackTrace();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package dev.rollczi.litecommands.handler.exception;

import dev.rollczi.litecommands.handler.result.ResultHandler;
import dev.rollczi.litecommands.handler.result.ResultHandlerChain;
import dev.rollczi.litecommands.invocation.Invocation;

public interface ExceptionHandler<SENDER, E extends Throwable> {
@FunctionalInterface
public interface ExceptionHandler<SENDER, E extends Throwable> extends ResultHandler<SENDER, E> {

void handle(Invocation<SENDER> invocation, E exception);
void handle(Invocation<SENDER> invocation, E exception, ResultHandlerChain<SENDER> chain);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.rollczi.litecommands.handler.exception.standard;

import dev.rollczi.litecommands.handler.exception.ExceptionHandler;
import dev.rollczi.litecommands.handler.result.ResultHandlerChain;
import dev.rollczi.litecommands.invocation.Invocation;

import java.lang.reflect.InvocationTargetException;

public class InvocationTargetExceptionHandler<SENDER> implements ExceptionHandler<SENDER, InvocationTargetException> {

@Override
public void handle(Invocation<SENDER> invocation, InvocationTargetException exception, ResultHandlerChain<SENDER> chain) {
Throwable cause = exception.getCause();

if (cause == null || cause.equals(exception)) {
chain.resolve(invocation, exception, ReflectiveOperationException.class);
return;
}

chain.resolve(invocation, cause);
}

}
Loading

0 comments on commit b9b5982

Please sign in to comment.