Skip to content

Commit 32fc723

Browse files
committed
Define CommandExceptionResolver bean order
- CommandParserExceptionResolver uses default bean order -100. - Backport #634 - Fixes #635
1 parent d7f0459 commit 32fc723

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

spring-shell-core/src/main/java/org/springframework/shell/command/CommandExceptionResolver.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.shell.command;
1717

18+
import org.springframework.core.Ordered;
19+
import org.springframework.core.annotation.Order;
20+
1821
/**
1922
* Interface to be implemented by objects that can resolve exceptions thrown
2023
* during command processing, in the typical case error response. Implementors
@@ -25,6 +28,11 @@
2528
*/
2629
public interface CommandExceptionResolver {
2730

31+
/**
32+
* Default precedence related use of {@link Ordered} and {@link Order}.
33+
*/
34+
int DEFAULT_PRECEDENCE = -100;
35+
2836
/**
2937
* Try to resolve the given exception that got thrown during command processing.
3038
*

spring-shell-core/src/main/java/org/springframework/shell/result/ResultHandlerConfig.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 the original author or authors.
2+
* Copyright 2017-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,10 @@
2121
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2222
import org.springframework.context.annotation.Bean;
2323
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.core.annotation.Order;
2425
import org.springframework.shell.TerminalSizeAware;
2526
import org.springframework.shell.command.CommandCatalog;
27+
import org.springframework.shell.command.CommandExceptionResolver;
2628
import org.springframework.shell.command.CommandParserExceptionResolver;
2729
import org.springframework.shell.context.ShellContext;
2830
import org.springframework.shell.jline.InteractiveShellRunner;
@@ -33,7 +35,7 @@
3335
* @author Eric Bottard
3436
* @author Janne Valkealahti
3537
*/
36-
@Configuration
38+
@Configuration(proxyBeanMethods = false)
3739
public class ResultHandlerConfig {
3840

3941
@Bean
@@ -58,6 +60,7 @@ public ParameterValidationExceptionResultHandler parameterValidationExceptionRes
5860
}
5961

6062
@Bean
63+
@Order(CommandExceptionResolver.DEFAULT_PRECEDENCE)
6164
public CommandParserExceptionResolver commandParserExceptionResolver() {
6265
return new CommandParserExceptionResolver();
6366
}

spring-shell-docs/src/main/asciidoc/using-shell-commands-exceptionhandling-resolving.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ you want to define exit code there.
4545
include::{snippets}/ErrorHandlingSnippets.java[tag=my-exception-class]
4646
----
4747
====
48+
49+
Some build in `CommandExceptionResolver` beans are registered to handle common
50+
exceptions thrown from command parsing. These are registered with _order_
51+
presedence defined in `CommandExceptionResolver.DEFAULT_PRECEDENCE`.
52+
As these beans are used in a given order, `@Order` annotation or `Ordered`
53+
interface from can be used just like in any other spring app. This
54+
is generally useful if you need to control your own beans to get used
55+
either before or after a defaults.

0 commit comments

Comments
 (0)