49 lines
1.8 KiB
Plaintext
49 lines
1.8 KiB
Plaintext
[[dynamic-command-exitcode-resolving]]
|
|
= Exception Resolving
|
|
|
|
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
|
|
|
|
Unhandled exceptions will bubble up into shell's `ResultHandlerService` and then eventually
|
|
handled by some instance of `ResultHandler`. Chain of `ExceptionResolver` implementations
|
|
can be used to resolve exceptions and gives you the flexibility to return a message to get
|
|
written into the console together with exit code which are wrapped within `CommandHandlingResult`.
|
|
`CommandHandlingResult` may contain a _message_ and/or _exit code_.
|
|
|
|
[source, java, indent=0]
|
|
----
|
|
include::{snippets}/ErrorHandlingSnippets.java[tag=my-exception-resolver-class]
|
|
----
|
|
|
|
`CommandExceptionResolver` implementations can be defined globally as beans:
|
|
|
|
[source, java, indent=0]
|
|
----
|
|
include::{snippets}/ErrorHandlingSnippets.java[tag=my-exception-resolver-class-as-bean]
|
|
----
|
|
|
|
or defined per `CommandRegistration` if it's applicable only to a particular command:
|
|
|
|
[source, java, indent=0]
|
|
----
|
|
include::{snippets}/ErrorHandlingSnippets.java[tag=example1]
|
|
----
|
|
|
|
NOTE: Resolvers defined with a command are handled before global resolvers.
|
|
|
|
|
|
You can use your own exception types which can also be instances of Spring Boot's `ExitCodeGenerator`
|
|
if you want to define exit code there:
|
|
|
|
[source, java, indent=0]
|
|
----
|
|
include::{snippets}/ErrorHandlingSnippets.java[tag=my-exception-class]
|
|
----
|
|
|
|
Some built-in `CommandExceptionResolver` beans are registered to handle common
|
|
exceptions thrown from command parsing. These are registered with _order_
|
|
precedence defined in `CommandExceptionResolver.DEFAULT_PRECEDENCE`.
|
|
As these beans are used in a given order, `@Order` annotation or `Ordered`
|
|
interface 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 default ones.
|