Support exception handling with annotated methods
- New annotations ExceptionResolver and ExitCode - New needed functionality is in classes ExceptionResolverMethodResolver and MethodCommandExceptionResolver. - Hook these annotations with StandardMethodTargetRegistrar and Shell classes - Fixes #597
This commit is contained in:
@@ -15,14 +15,21 @@
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.jline.terminal.Terminal;
|
||||
|
||||
import org.springframework.boot.ExitCodeGenerator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.command.CommandExceptionResolver;
|
||||
import org.springframework.shell.command.CommandHandlingResult;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.command.annotation.ExceptionResolver;
|
||||
import org.springframework.shell.command.annotation.ExitCode;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
import org.springframework.shell.standard.ShellMethod;
|
||||
|
||||
/**
|
||||
* Commands used for e2e test.
|
||||
@@ -32,8 +39,55 @@ import org.springframework.shell.standard.ShellComponent;
|
||||
@ShellComponent
|
||||
public class ErrorHandlingCommands extends BaseE2ECommands {
|
||||
|
||||
@ShellMethod(key = LEGACY_ANNO + "error-handling", group = GROUP)
|
||||
String testErrorHandling(String arg1) throws IOException {
|
||||
if ("throw1".equals(arg1)) {
|
||||
throw new CustomException1();
|
||||
}
|
||||
if ("throw2".equals(arg1)) {
|
||||
throw new CustomException2(11);
|
||||
}
|
||||
if ("throw3".equals(arg1)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
if ("throw4".equals(arg1)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if ("throw5".equals(arg1)) {
|
||||
throw new CustomException3();
|
||||
}
|
||||
if ("throw6".equals(arg1)) {
|
||||
throw new CustomException4();
|
||||
}
|
||||
return "Hello " + arg1;
|
||||
}
|
||||
|
||||
@ExceptionResolver({ CustomException1.class })
|
||||
CommandHandlingResult errorHandler1(CustomException1 e) {
|
||||
return CommandHandlingResult.of("Hi, handled custom exception\n", 42);
|
||||
}
|
||||
|
||||
@ExceptionResolver
|
||||
CommandHandlingResult errorHandler2(IllegalArgumentException e) {
|
||||
return CommandHandlingResult.of("Hi, handled illegal exception\n", 42);
|
||||
}
|
||||
|
||||
@ExceptionResolver({ CustomException3.class })
|
||||
@ExitCode(3)
|
||||
String errorHandler3(CustomException3 e) {
|
||||
return "Hi, handled custom exception 3\n";
|
||||
}
|
||||
|
||||
@ExceptionResolver({ CustomException4.class })
|
||||
@ExitCode(code = 4)
|
||||
void errorHandler3(CustomException4 e, Terminal terminal) {
|
||||
PrintWriter writer = terminal.writer();
|
||||
writer.println(String.format("Hi, handled custom exception %s", e));
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testErrorHandlingRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
CommandRegistration testErrorHandlingRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "error-handling")
|
||||
.group(GROUP)
|
||||
@@ -56,6 +110,16 @@ public class ErrorHandlingCommands extends BaseE2ECommands {
|
||||
if ("throw3".equals(arg1)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
if ("throw4".equals(arg1)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if ("throw5".equals(arg1)) {
|
||||
throw new CustomException3();
|
||||
}
|
||||
if ("throw6".equals(arg1)) {
|
||||
throw new CustomException4();
|
||||
}
|
||||
|
||||
return "Hello " + arg1;
|
||||
})
|
||||
.and()
|
||||
@@ -79,13 +143,27 @@ public class ErrorHandlingCommands extends BaseE2ECommands {
|
||||
}
|
||||
}
|
||||
|
||||
private static class CustomException3 extends RuntimeException {
|
||||
}
|
||||
|
||||
private static class CustomException4 extends RuntimeException {
|
||||
}
|
||||
|
||||
private static class CustomExceptionResolver implements CommandExceptionResolver {
|
||||
|
||||
@Override
|
||||
public CommandHandlingResult resolve(Exception e) {
|
||||
if (e instanceof CustomException1) {
|
||||
return CommandHandlingResult.of("Hi, handled exception\n", 42);
|
||||
return CommandHandlingResult.of("Hi, handled custom exception\n", 42);
|
||||
}
|
||||
if (e instanceof CustomException3) {
|
||||
return CommandHandlingResult.of("Hi, handled custom exception 3\n", 3);
|
||||
}
|
||||
if (e instanceof CustomException4) {
|
||||
return CommandHandlingResult.of("Hi, handled custom exception\n", 42);
|
||||
}
|
||||
if (e instanceof IllegalArgumentException) {
|
||||
return CommandHandlingResult.of("Hi, handled illegal exception\n", 42);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user