33
33
import org .kie .kogito .process .workitem .InvalidLifeCyclePhaseException ;
34
34
import org .kie .kogito .process .workitem .InvalidTransitionException ;
35
35
import org .kie .kogito .process .workitem .NotAuthorizedException ;
36
+ import org .kie .kogito .process .workitem .WorkItemExecutionException ;
36
37
37
38
public abstract class BaseExceptionsHandler <T > {
38
39
@@ -48,9 +49,9 @@ public abstract class BaseExceptionsHandler<T> {
48
49
49
50
private static class FunctionHolder <T , R > {
50
51
private final Function <Exception , R > contentGenerator ;
51
- private final Function <R , T > responseGenerator ;
52
+ private final Function <Exception , Function < R , T > > responseGenerator ;
52
53
53
- public FunctionHolder (Function <Exception , R > contentGenerator , Function <R , T > responseGenerator ) {
54
+ public FunctionHolder (Function <Exception , R > contentGenerator , Function <Exception , Function < R , T > > responseGenerator ) {
54
55
this .contentGenerator = contentGenerator ;
55
56
this .responseGenerator = responseGenerator ;
56
57
}
@@ -59,20 +60,20 @@ public Function<Exception, R> getContentGenerator() {
59
60
return contentGenerator ;
60
61
}
61
62
62
- public Function <R , T > getResponseGenerator () {
63
+ public Function <Exception , Function < R , T > > getResponseGenerator () {
63
64
return responseGenerator ;
64
65
}
65
66
}
66
67
67
- private final FunctionHolder <T , Exception > defaultHolder = new FunctionHolder <>(ex -> ex , BaseExceptionsHandler .this ::internalError );
68
+ private final FunctionHolder <T , Exception > defaultHolder = new FunctionHolder <>(ex -> ex , ex -> BaseExceptionsHandler .this ::internalError );
68
69
69
70
protected BaseExceptionsHandler () {
70
71
mapper = new HashMap <>();
71
72
mapper .put (InvalidLifeCyclePhaseException .class , new FunctionHolder <>(
72
- ex -> Collections .singletonMap (MESSAGE , ex .getMessage ()), BaseExceptionsHandler .this ::badRequest ));
73
+ ex -> Collections .singletonMap (MESSAGE , ex .getMessage ()), ex -> BaseExceptionsHandler .this ::badRequest ));
73
74
74
75
mapper .put (InvalidTransitionException .class , new FunctionHolder <>(
75
- ex -> Collections .singletonMap (MESSAGE , ex .getMessage ()), BaseExceptionsHandler .this ::badRequest ));
76
+ ex -> Collections .singletonMap (MESSAGE , ex .getMessage ()), ex -> BaseExceptionsHandler .this ::badRequest ));
76
77
77
78
mapper .put (NodeInstanceNotFoundException .class , new FunctionHolder <>(
78
79
ex -> {
@@ -82,7 +83,7 @@ protected BaseExceptionsHandler() {
82
83
response .put (PROCESS_INSTANCE_ID , exception .getProcessInstanceId ());
83
84
response .put (NODE_INSTANCE_ID , exception .getNodeInstanceId ());
84
85
return response ;
85
- }, BaseExceptionsHandler .this ::notFound ));
86
+ }, ex -> BaseExceptionsHandler .this ::notFound ));
86
87
87
88
mapper .put (NodeNotFoundException .class , new FunctionHolder <>(
88
89
ex -> {
@@ -92,10 +93,10 @@ protected BaseExceptionsHandler() {
92
93
response .put (PROCESS_INSTANCE_ID , exception .getProcessInstanceId ());
93
94
response .put (NODE_ID , exception .getNodeId ());
94
95
return response ;
95
- }, BaseExceptionsHandler .this ::notFound ));
96
+ }, ex -> BaseExceptionsHandler .this ::notFound ));
96
97
97
98
mapper .put (NotAuthorizedException .class , new FunctionHolder <>(
98
- ex -> Collections .singletonMap (MESSAGE , ex .getMessage ()), BaseExceptionsHandler .this ::forbidden ));
99
+ ex -> Collections .singletonMap (MESSAGE , ex .getMessage ()), ex -> BaseExceptionsHandler .this ::forbidden ));
99
100
100
101
mapper .put (ProcessInstanceDuplicatedException .class , new FunctionHolder <>(
101
102
ex -> {
@@ -104,7 +105,7 @@ protected BaseExceptionsHandler() {
104
105
response .put (MESSAGE , exception .getMessage ());
105
106
response .put (PROCESS_INSTANCE_ID , exception .getProcessInstanceId ());
106
107
return response ;
107
- }, BaseExceptionsHandler .this ::conflict ));
108
+ }, ex -> BaseExceptionsHandler .this ::conflict ));
108
109
109
110
mapper .put (ProcessInstanceExecutionException .class , new FunctionHolder <>(
110
111
ex -> {
@@ -114,7 +115,7 @@ protected BaseExceptionsHandler() {
114
115
response .put (FAILED_NODE_ID , exception .getFailedNodeId ());
115
116
response .put (MESSAGE , exception .getErrorMessage ());
116
117
return response ;
117
- }, BaseExceptionsHandler .this ::internalError ));
118
+ }, ex -> BaseExceptionsHandler .this ::internalError ));
118
119
119
120
mapper .put (ProcessInstanceNotFoundException .class , new FunctionHolder <>(
120
121
ex -> {
@@ -123,12 +124,12 @@ protected BaseExceptionsHandler() {
123
124
response .put (MESSAGE , exception .getMessage ());
124
125
response .put (PROCESS_INSTANCE_ID , exception .getProcessInstanceId ());
125
126
return response ;
126
- }, BaseExceptionsHandler .this ::notFound ));
127
+ }, ex -> BaseExceptionsHandler .this ::notFound ));
127
128
128
129
mapper .put (WorkItemNotFoundException .class , new FunctionHolder <>(ex -> {
129
130
WorkItemNotFoundException exception = (WorkItemNotFoundException ) ex ;
130
131
return Map .of (MESSAGE , exception .getMessage (), TASK_ID , exception .getWorkItemId ());
131
- }, BaseExceptionsHandler .this ::notFound ));
132
+ }, ex -> BaseExceptionsHandler .this ::notFound ));
132
133
133
134
mapper .put (VariableViolationException .class , new FunctionHolder <>(
134
135
ex -> {
@@ -138,9 +139,28 @@ protected BaseExceptionsHandler() {
138
139
response .put (PROCESS_INSTANCE_ID , exception .getProcessInstanceId ());
139
140
response .put (VARIABLE , exception .getVariableName ());
140
141
return response ;
141
- }, BaseExceptionsHandler .this ::badRequest ));
142
+ }, ex -> BaseExceptionsHandler .this ::badRequest ));
142
143
143
- mapper .put (IllegalArgumentException .class , new FunctionHolder <>(ex -> Collections .singletonMap (MESSAGE , ex .getMessage ()), BaseExceptionsHandler .this ::badRequest ));
144
+ mapper .put (WorkItemExecutionException .class , new FunctionHolder <>(
145
+ ex -> Map .of (MESSAGE , ex .getMessage ()),
146
+ ex -> fromErrorCode (((WorkItemExecutionException ) ex ).getErrorCode ())));
147
+
148
+ mapper .put (IllegalArgumentException .class , new FunctionHolder <>(ex -> Collections .singletonMap (MESSAGE , ex .getMessage ()), ex -> BaseExceptionsHandler .this ::badRequest ));
149
+ }
150
+
151
+ private <R > Function <R , T > fromErrorCode (String errorCode ) {
152
+ switch (errorCode ) {
153
+ case "400" :
154
+ return this ::badRequest ;
155
+ case "403" :
156
+ return this ::forbidden ;
157
+ case "404" :
158
+ return this ::notFound ;
159
+ case "409" :
160
+ return this ::conflict ;
161
+ default :
162
+ return this ::internalError ;
163
+ }
144
164
}
145
165
146
166
protected abstract <R > T badRequest (R body );
@@ -156,8 +176,8 @@ protected BaseExceptionsHandler() {
156
176
public <R extends Exception , U > T mapException (R exception ) {
157
177
FunctionHolder <T , U > holder = (FunctionHolder <T , U >) mapper .getOrDefault (exception .getClass (), defaultHolder );
158
178
U body = holder .getContentGenerator ().apply (exception );
159
- if (exception instanceof ProcessInstanceExecutionException ) {
160
- Throwable rootCause = (( ProcessInstanceExecutionException ) exception ) .getCause ();
179
+ if (exception instanceof ProcessInstanceExecutionException || exception instanceof WorkItemExecutionException ) {
180
+ Throwable rootCause = exception .getCause ();
161
181
162
182
while (rootCause != null ) {
163
183
if (mapper .containsKey (rootCause .getClass ())) {
@@ -167,6 +187,6 @@ public <R extends Exception, U> T mapException(R exception) {
167
187
rootCause = rootCause .getCause ();
168
188
}
169
189
}
170
- return holder .getResponseGenerator ().apply (body );
190
+ return holder .getResponseGenerator ().apply (exception ). apply ( body );
171
191
}
172
192
}
0 commit comments