Exception Resolving

未处理的异常将冒泡到 shell 的 ResultHandlerService 中,然后最终由 ResultHandler 的某个实例处理。可以将 ExceptionResolver 实现的链用于解决异常,并为您提供灵活性,以便返回消息以与 CommandHandlingResult 中包含的退出代码一起写入控制台。CommandHandlingResult 可能包含 message 和/或 exit code

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 flexibility to return message to get written into console together with exit code which are wrapped within CommandHandlingResult. CommandHandlingResult may contain a message and/or exit code.

Unresolved include directive in modules/ROOT/pages/commands/exceptionhandling/resolving.adoc - include::../../test/java/org/springframework/shell/docs/ErrorHandlingSnippets.java[]

可以将 CommandExceptionResolver 实现全局定义为 bean。

CommandExceptionResolver implementations can be defined globally as bean.

Unresolved include directive in modules/ROOT/pages/commands/exceptionhandling/resolving.adoc - include::../../test/java/org/springframework/shell/docs/ErrorHandlingSnippets.java[]

或者每个 CommandRegistration 定义(如果仅适用于特定命令本身)。

or defined per CommandRegistration if it’s applicable only for a particular command itself.

Unresolved include directive in modules/ROOT/pages/commands/exceptionhandling/resolving.adoc - include::../../test/java/org/springframework/shell/docs/ErrorHandlingSnippets.java[]

使用命令定义的解析器在全局解析器之前处理。

Resolvers defined with a command are handled before global resolvers.

使用您自己的异常类型,它也可以是 boot 的 ExitCodeGenerator 的实例,如果您想要在那里定义退出代码。

Use you own exception types which can also be an instance of boot’s ExitCodeGenerator if you want to define exit code there.

Unresolved include directive in modules/ROOT/pages/commands/exceptionhandling/resolving.adoc - include::../../test/java/org/springframework/shell/docs/ErrorHandlingSnippets.java[]

某些内置 CommandExceptionResolver bean 已注册为处理从命令解析中引发的常见异常。这些使用 order 注册,优先级在 CommandExceptionResolver.DEFAULT_PRECEDENCE 中定义。由于这些 bean 以给定的顺序使用,因此 @Order 注解或 Ordered 可以与任何其他 Spring 应用程序一样使用。如果您需要控制自己的 bean 以在默认值之前或之后使用,这通常很有用。

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.