Define CommandExceptionResolver bean order

- CommandParserExceptionResolver uses default bean
  order -100.
- Backport #634
- Fixes #635
This commit is contained in:
Janne Valkealahti
2023-01-25 17:57:13 +00:00
parent d7f0459874
commit 32fc72329b
3 changed files with 22 additions and 3 deletions

View File

@@ -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.
*

View File

@@ -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();
}

View File

@@ -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.