17
17
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
18
18
class HookSupport {
19
19
20
- public void errorHooks (FlagValueType flagValueType , HookContext hookCtx , Exception e , List <Hook > hooks ,
20
+ public EvaluationContext beforeHooks (FlagValueType flagValueType , HookContext hookCtx , List <Hook > hooks ,
21
21
Map <String , Object > hints ) {
22
- executeHooks (flagValueType , hooks , "error" , hook -> hook .error (hookCtx , e , hints ));
22
+ Stream <EvaluationContext > result = callBeforeHooks (flagValueType , hookCtx , hooks , hints );
23
+ return hookCtx .getCtx ().merge (
24
+ result .reduce (hookCtx .getCtx (), (EvaluationContext accumulated , EvaluationContext current ) -> {
25
+ return accumulated .merge (current );
26
+ }));
27
+ }
28
+
29
+ public void afterHooks (FlagValueType flagValueType , HookContext hookContext , FlagEvaluationDetails details ,
30
+ List <Hook > hooks , Map <String , Object > hints ) {
31
+ executeHooksUnchecked (flagValueType , hooks , hook -> hook .after (hookContext , details , hints ));
23
32
}
24
33
25
34
public void afterAllHooks (FlagValueType flagValueType , HookContext hookCtx , List <Hook > hooks ,
26
35
Map <String , Object > hints ) {
27
36
executeHooks (flagValueType , hooks , "finally" , hook -> hook .finallyAfter (hookCtx , hints ));
28
37
}
29
38
30
- public void afterHooks (FlagValueType flagValueType , HookContext hookContext , FlagEvaluationDetails details ,
31
- List < Hook > hooks , Map <String , Object > hints ) {
32
- executeHooksUnchecked (flagValueType , hooks , hook -> hook .after ( hookContext , details , hints ));
39
+ public void errorHooks (FlagValueType flagValueType , HookContext hookCtx , Exception e , List < Hook > hooks ,
40
+ Map <String , Object > hints ) {
41
+ executeHooks (flagValueType , hooks , "error" , hook -> hook .error ( hookCtx , e , hints ));
33
42
}
34
43
35
44
private <T > void executeHooks (
@@ -44,6 +53,17 @@ private <T> void executeHooks(
44
53
}
45
54
}
46
55
56
+ // before, error, and finally hooks shouldn't throw
57
+ private <T > void executeChecked (Hook <T > hook , Consumer <Hook <T >> hookCode , String hookMethod ) {
58
+ try {
59
+ hookCode .accept (hook );
60
+ } catch (Exception exception ) {
61
+ log .error ("Unhandled exception when running {} hook {} (only 'after' hooks should throw)" , hookMethod ,
62
+ hook .getClass (), exception );
63
+ }
64
+ }
65
+
66
+ // after hooks can throw in order to do validation
47
67
private <T > void executeHooksUnchecked (
48
68
FlagValueType flagValueType , List <Hook > hooks ,
49
69
Consumer <Hook <T >> hookCode ) {
@@ -55,23 +75,6 @@ private <T> void executeHooksUnchecked(
55
75
}
56
76
}
57
77
58
- private <T > void executeChecked (Hook <T > hook , Consumer <Hook <T >> hookCode , String hookMethod ) {
59
- try {
60
- hookCode .accept (hook );
61
- } catch (Exception exception ) {
62
- log .error ("Exception when running {} hooks {}" , hookMethod , hook .getClass (), exception );
63
- }
64
- }
65
-
66
- public EvaluationContext beforeHooks (FlagValueType flagValueType , HookContext hookCtx , List <Hook > hooks ,
67
- Map <String , Object > hints ) {
68
- Stream <EvaluationContext > result = callBeforeHooks (flagValueType , hookCtx , hooks , hints );
69
- return hookCtx .getCtx ().merge (
70
- result .reduce (hookCtx .getCtx (), (EvaluationContext accumulated , EvaluationContext current ) -> {
71
- return accumulated .merge (current );
72
- }));
73
- }
74
-
75
78
private Stream <EvaluationContext > callBeforeHooks (FlagValueType flagValueType , HookContext hookCtx ,
76
79
List <Hook > hooks , Map <String , Object > hints ) {
77
80
// These traverse backwards from normal.
0 commit comments