diff --git a/spring-shell-core/src/main/java/org/springframework/shell/command/CommandExceptionResolver.java b/spring-shell-core/src/main/java/org/springframework/shell/command/CommandExceptionResolver.java index 2efcb977..d29a54a2 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/command/CommandExceptionResolver.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/command/CommandExceptionResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * Copyright 2022-2023 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. @@ -15,6 +15,9 @@ */ package org.springframework.shell.command; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; + /** * Interface to be implemented by objects that can resolve exceptions thrown * during command processing, in the typical case error response. Implementors @@ -25,6 +28,11 @@ package org.springframework.shell.command; */ public interface CommandExceptionResolver { + /** + * Default precedence related use of {@link Ordered} and {@link Order}. + */ + int DEFAULT_PRECEDENCE = -100; + /** * Try to resolve the given exception that got thrown during command processing. * diff --git a/spring-shell-core/src/main/java/org/springframework/shell/result/ResultHandlerConfig.java b/spring-shell-core/src/main/java/org/springframework/shell/result/ResultHandlerConfig.java index 66a44555..12f8e536 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/result/ResultHandlerConfig.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/result/ResultHandlerConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2022 the original author or authors. + * Copyright 2017-2023 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. @@ -21,8 +21,10 @@ import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; import org.springframework.shell.TerminalSizeAware; import org.springframework.shell.command.CommandCatalog; +import org.springframework.shell.command.CommandExceptionResolver; import org.springframework.shell.command.CommandParserExceptionResolver; import org.springframework.shell.context.ShellContext; import org.springframework.shell.jline.InteractiveShellRunner; @@ -33,7 +35,7 @@ import org.springframework.shell.jline.InteractiveShellRunner; * @author Eric Bottard * @author Janne Valkealahti */ -@Configuration +@Configuration(proxyBeanMethods = false) public class ResultHandlerConfig { @Bean @@ -58,6 +60,7 @@ public class ResultHandlerConfig { } @Bean + @Order(CommandExceptionResolver.DEFAULT_PRECEDENCE) public CommandParserExceptionResolver commandParserExceptionResolver() { return new CommandParserExceptionResolver(); } diff --git a/spring-shell-docs/src/main/asciidoc/using-shell-commands-exceptionhandling-resolving.adoc b/spring-shell-docs/src/main/asciidoc/using-shell-commands-exceptionhandling-resolving.adoc index f85af205..eb4998bb 100644 --- a/spring-shell-docs/src/main/asciidoc/using-shell-commands-exceptionhandling-resolving.adoc +++ b/spring-shell-docs/src/main/asciidoc/using-shell-commands-exceptionhandling-resolving.adoc @@ -45,3 +45,11 @@ you want to define exit code there. include::{snippets}/ErrorHandlingSnippets.java[tag=my-exception-class] ---- ==== + +Some build in `CommandExceptionResolver` beans are registered to handle common +exceptions thrown from command parsing. These are registered with _order_ +presedence defined in `CommandExceptionResolver.DEFAULT_PRECEDENCE`. +As these beans are used in a given order, `@Order` annotation or `Ordered` +interface from can be used just like in any other spring app. This +is generally useful if you need to control your own beans to get used +either before or after a defaults.