Implement more flexible error handling
- Add exception handling around new interface CommandExceptionResolver which allows to define a chain of resolvers to process errors before exception is bubbled up to result handlers. - Will be foundation to add more sophisticated error handling features compared to what spring itself have for rest layer. - Resolver returns CommandHandlingResult holder which further can be used to make a choice what to print into console and if spesific exit code should be used in non-interactive mode. - Exception handling can be defined globally and per command giving a change for user to customise i.e. error thrown by parser. - CommandParserExceptionResolver replaces CommandParserExceptionsExceptionResultHandler and provides more meaninful message for missing options. - Fixes #503
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
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.standard.ShellComponent;
|
||||
|
||||
/**
|
||||
* Commands used for e2e test.
|
||||
*
|
||||
* @author Janne Valkealahti
|
||||
*/
|
||||
@ShellComponent
|
||||
public class ErrorHandlingCommands extends BaseE2ECommands {
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testErrorHandlingRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
.command(REG, "error-handling")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.required()
|
||||
.and()
|
||||
.withErrorHandling()
|
||||
.resolver(new CustomExceptionResolver())
|
||||
.and()
|
||||
.withTarget()
|
||||
.function(ctx -> {
|
||||
String arg1 = ctx.getOptionValue("arg1");
|
||||
if ("throw1".equals(arg1)) {
|
||||
throw new CustomException1();
|
||||
}
|
||||
if ("throw2".equals(arg1)) {
|
||||
throw new CustomException2(11);
|
||||
}
|
||||
if ("throw3".equals(arg1)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
return "Hello " + arg1;
|
||||
})
|
||||
.and()
|
||||
.build();
|
||||
}
|
||||
|
||||
private static class CustomException1 extends RuntimeException {
|
||||
}
|
||||
|
||||
private static class CustomException2 extends RuntimeException implements ExitCodeGenerator {
|
||||
|
||||
private int code;
|
||||
|
||||
CustomException2(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getExitCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public class RequiredValueCommands extends BaseE2ECommands {
|
||||
|
||||
@ShellMethod(key = LEGACY_ANNO + "required-value", group = GROUP)
|
||||
public String testRequiredValueAnnotation(
|
||||
@ShellOption String arg1
|
||||
@ShellOption(help = "Desc arg1") String arg1
|
||||
) {
|
||||
return "Hello " + arg1;
|
||||
}
|
||||
@@ -43,6 +43,7 @@ public class RequiredValueCommands extends BaseE2ECommands {
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.description("Desc arg1")
|
||||
.required()
|
||||
.and()
|
||||
.withTarget()
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
import org.springframework.shell.standard.ShellMethod;
|
||||
import org.springframework.shell.standard.ShellOption;
|
||||
|
||||
/**
|
||||
* Commands used for e2e test.
|
||||
*
|
||||
* @author Janne Valkealahti
|
||||
*/
|
||||
@ShellComponent
|
||||
public class ValidatedValueCommands extends BaseE2ECommands {
|
||||
|
||||
@ShellMethod(key = LEGACY_ANNO + "validated-value", group = GROUP)
|
||||
public String testValidatedValueAnnotation(
|
||||
@ShellOption @Min(value = 1) Integer arg1,
|
||||
@ShellOption @Min(value = 1) Integer arg2
|
||||
) {
|
||||
return "Hello " + arg1;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testValidatedValueRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
.command(REG, "validated-value")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.type(Integer.class)
|
||||
.required()
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg2")
|
||||
.type(Integer.class)
|
||||
.required()
|
||||
.and()
|
||||
.withTarget()
|
||||
.function(ctx -> {
|
||||
Integer arg1 = ctx.getOptionValue("arg1");
|
||||
return "Hello " + arg1;
|
||||
})
|
||||
.and()
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user