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:
Janne Valkealahti
2022-10-14 10:46:27 +01:00
parent 3fe26025cd
commit 4c48017a97
23 changed files with 999 additions and 102 deletions

View File

@@ -20,11 +20,13 @@ import java.util.List;
import java.util.function.Function;
import org.springframework.boot.ExitCodeExceptionMapper;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.shell.command.CommandExecution;
import org.springframework.shell.exit.ExitCodeExceptionProvider;
import org.springframework.shell.exit.ExitCodeMappings;
/**
@@ -47,6 +49,12 @@ public class ExitCodeAutoConfiguration {
return new ShellExitCodeMappingsExceptionMapper();
}
@Bean
@ConditionalOnMissingBean
public ExitCodeExceptionProvider exitCodeExceptionProvider() {
return (exception, code) -> new ShellExitCodeException(exception, code);
}
static class ShellExitCodeExceptionMapper implements ExitCodeExceptionMapper {
@Override
@@ -85,4 +93,19 @@ public class ExitCodeAutoConfiguration {
return exitCode;
}
}
static class ShellExitCodeException extends RuntimeException implements ExitCodeGenerator {
private int code;
ShellExitCodeException(Throwable throwable, int code) {
super(throwable);
this.code = code;
}
@Override
public int getExitCode() {
return code;
}
}
}