2121import jsheets .SnippetSources ;
2222import jsheets .StartEvaluationRequest ;
2323import jsheets .evaluation .Evaluation ;
24+ import jsheets .evaluation .failure .FailedEvaluation ;
2425import jsheets .evaluation .shell .environment .ExecutionEnvironment ;
2526import jsheets .evaluation .shell .execution .ExecutionMethod ;
26- import jsheets .source .SharedSources ;
2727
2828final class ShellEvaluation implements Evaluation {
2929 private enum Stage { Initial , Starting , Evaluating , Terminated }
@@ -33,14 +33,12 @@ private enum Stage { Initial, Starting, Evaluating, Terminated }
3333 private volatile JShell shell ;
3434 private volatile ExecutionMethod executionMethod ;
3535 private final Evaluation .Listener listener ;
36- private final SharedSources sources ;
3736 private final ExecutionEnvironment environment ;
3837 private final ExecutionMethod .Factory executionMethodFactory ;
3938 private final MessageOutput messageOutput ;
4039 private final AtomicReference <Stage > stage = new AtomicReference <>(Stage .Initial );
4140
4241 ShellEvaluation (
43- Clock clock ,
4442 ExecutionEnvironment environment ,
4543 ExecutionMethod .Factory executionMethodFactory ,
4644 Evaluation .Listener listener ,
@@ -50,7 +48,6 @@ private enum Stage { Initial, Starting, Evaluating, Terminated }
5048 this .messageOutput = messageOutput ;
5149 this .executionMethodFactory = executionMethodFactory ;
5250 this .environment = environment ;
53- sources = SharedSources .createEmpty (clock );
5451 }
5552
5653 public void start (StartEvaluationRequest request ) {
@@ -82,19 +79,49 @@ private void evaluateComponent(
8279 reportEvent (component .getId (), snippet , response );
8380 }
8481 messageOutput .flush ();
85- } catch (Exception failedEvaluation ) {
86- log .atWarning ()
87- .atMostEvery (5 , TimeUnit .SECONDS )
88- .withCause (failedEvaluation ).log ("shell evaluation failed" );
89- response .addError (
90- EvaluationError .newBuilder ()
91- .setComponentId (component .getId ())
92- .setKind ("internal" )
93- .build ()
94- );
82+ } catch (Throwable failedEvaluation ) {
83+ reportError (component , response , failedEvaluation );
9584 }
9685 }
9786
87+ private void reportError (
88+ SnippetSources .CodeComponent component ,
89+ EvaluateResponse .Builder response ,
90+ Throwable failure
91+ ) {
92+ FailedEvaluation .capture (failure ).ifPresentOrElse (
93+ value -> reportFailedEvaluation (component , response , value ),
94+ () -> reportInternalFailure (component , response , failure )
95+ );
96+ }
97+
98+ private void reportFailedEvaluation (
99+ SnippetSources .CodeComponent component ,
100+ EvaluateResponse .Builder response ,
101+ FailedEvaluation failedEvaluation
102+ ) {
103+ failedEvaluation .describe (Locale .ENGLISH )
104+ .distinct ()
105+ .map (error -> error .toBuilder ().setComponentId (component .getId ()))
106+ .forEach (response ::addError );
107+ }
108+
109+ private void reportInternalFailure (
110+ SnippetSources .CodeComponent component ,
111+ EvaluateResponse .Builder response ,
112+ Throwable failure
113+ ) {
114+ log .atWarning ()
115+ .atMostEvery (5 , TimeUnit .SECONDS )
116+ .withCause (failure ).log ("shell evaluation failed" );
117+ response .addError (
118+ EvaluationError .newBuilder ()
119+ .setComponentId (component .getId ())
120+ .setKind ("internal" )
121+ .build ()
122+ );
123+ }
124+
98125 private void reportEvent (
99126 String componentId ,
100127 SnippetEvent event ,
@@ -108,6 +135,7 @@ private void reportEvent(
108135 response .addResult (
109136 EvaluationResult .newBuilder ()
110137 .setComponentId (componentId )
138+ .setKind (EvaluationResult .Kind .INFO )
111139 .setOutput (event .value ())
112140 .build ()
113141 );
@@ -159,7 +187,6 @@ private void cleanUp() {
159187 public String toString () {
160188 return MoreObjects .toStringHelper (this )
161189 .add ("shell" , shell )
162- .add ("sources" , sources )
163190 .add ("environment" , environment )
164191 .add ("stage" , stage )
165192 .toString ();
0 commit comments