出口代码映射
出口代码的默认行为如下:
-
命令选项解析错误会导致代码
2 -
任何泛误都会导致编码
1 -
显然,在其他情况下,将导致编码
0
每指挥注册可以自定义异常与退出代码之间的映射。Spring shell 采用了类似的方法Spring靴关于退出代码,简单来说整合进去。
假设下面显示的异常会从命令中抛出:
static class MyException extends RuntimeException {
private final int code;
MyException(String msg, int code) {
super(msg);
this.code = code;
}
public int getCode() {
return code;
}
}
可以定义一个映射函数可投掷还有退出代码。你也可以直接配置类去退出代码,这只是配置中的一个语法糖。
CommandRegistration.builder()
.withExitCode()
.map(MyException.class, 3)
.map(t -> {
if (t instanceof MyException) {
return ((MyException) t).getCode();
}
return 0;
})
.and()
.build();
| 出口代码无法通过基于注释的配置进行自定义 |