diff --git a/src/main/java/org/springframework/shell/ExecutionProcessor.java b/src/main/java/org/springframework/shell/ExecutionProcessor.java index 90c01af3..a2b632df 100644 --- a/src/main/java/org/springframework/shell/ExecutionProcessor.java +++ b/src/main/java/org/springframework/shell/ExecutionProcessor.java @@ -37,9 +37,19 @@ public interface ExecutionProcessor extends CommandMarker { ParseResult beforeInvocation(ParseResult invocationContext); /** - * Method called after invoking the target command (described by {@link ParseResult}). + * Method called after successfully invoking the target command (described by {@link ParseResult}). * * @param invocationContext target command context + * @param result the invocation result */ - void afterInvocation(ParseResult invocationContext); + void afterReturningInvocation(ParseResult invocationContext, Object result); + + /** + * Method called after invoking the target command (described by {@link ParseResult}) had thrown an exception . + * + * @param invocationContext target command context + * @param thrown the thrown object + */ + void afterThrowingInvocation(ParseResult invocationContext, Throwable thrown); + } diff --git a/src/main/java/org/springframework/shell/SimpleExecutionStrategy.java b/src/main/java/org/springframework/shell/SimpleExecutionStrategy.java index 3d20dd02..79925aec 100644 --- a/src/main/java/org/springframework/shell/SimpleExecutionStrategy.java +++ b/src/main/java/org/springframework/shell/SimpleExecutionStrategy.java @@ -27,9 +27,18 @@ public class SimpleExecutionStrategy implements ExecutionStrategy { ExecutionProcessor processor = ((ExecutionProcessor) target); parseResult = processor.beforeInvocation(parseResult); try { - return invoke(parseResult); - } finally { - processor.afterInvocation(parseResult); + Object result = invoke(parseResult); + processor.afterReturningInvocation(parseResult, result); + return result; + } catch (Throwable th) { + processor.afterThrowingInvocation(parseResult, th); + if (th instanceof Error) { + throw ((Error) th); + } + if (th instanceof RuntimeException) { + throw ((RuntimeException) th); + } + throw new RuntimeException(th); } } else { diff --git a/src/test/java/org/springframework/shell/SimpleExecutionStrategyTest.java b/src/test/java/org/springframework/shell/SimpleExecutionStrategyTest.java index 8bedc23d..17975c13 100644 --- a/src/test/java/org/springframework/shell/SimpleExecutionStrategyTest.java +++ b/src/test/java/org/springframework/shell/SimpleExecutionStrategyTest.java @@ -46,9 +46,13 @@ public class SimpleExecutionStrategyTest { return (beforeReturn == null ? invocationContext : beforeReturn); } - public void afterInvocation(ParseResult invocationContext) { + public void afterReturningInvocation(ParseResult invocationContext, Object result) { after = invocationContext; } + + public void afterThrowingInvocation(ParseResult invocationContext, Throwable thrown) { + // + } } private SimpleExecutionStrategy execution = new SimpleExecutionStrategy();