Skip to content

Commit 5440451

Browse files
committed
Merge branch 'fix_actions_on_parent_error_handler'
2 parents 803f8ad + bb7f7b6 commit 5440451

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

errorhandler/src/main/java/com/workable/errorhandler/ErrorHandler.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ private ErrorHandler() {
6262
this.otherwiseActions = new ArrayList<>();
6363
this.alwaysActions = new ArrayList<>();
6464
this.errorCodeMap = new HashMap<>();
65+
this.localContext = new ThreadLocal<Context>(){
66+
@Override
67+
protected Context initialValue() {
68+
return new Context();
69+
}
70+
};
6571
}
6672

6773
/**
@@ -273,7 +279,7 @@ public void run(BlockExecutor blockExecutor) {
273279
try {
274280
blockExecutor.invoke();
275281
} catch (Exception exception) {
276-
handle(exception);
282+
handle(exception, localContext);
277283
}
278284
}
279285

@@ -283,12 +289,7 @@ public void run(BlockExecutor blockExecutor) {
283289
* @param error the error as a {@link Throwable}
284290
*/
285291
public void handle(Throwable error) {
286-
this.handle(error, new ThreadLocal<Context>() {
287-
@Override
288-
protected Context initialValue() {
289-
return new Context();
290-
}
291-
});
292+
this.handle(error, localContext);
292293
}
293294

294295
/**

errorhandler/src/test/java/com/workable/errorhandler/ErrorHandlerTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ interface ActionDelegate {
3434

3535
void defaultAction2();
3636

37+
void defaultAction3();
38+
3739
void defaultOtherwise();
3840

3941
void defaultAlways();
@@ -62,6 +64,7 @@ protected void setUp() {
6264
})
6365
.on(FooException.class, (throwable, errorHandler) -> actionDelegateMock.defaultAction1())
6466
.on(500, (throwable, errorHandler) -> actionDelegateMock.defaultAction2())
67+
.on("closed:bar", (throwable, errorHandler) -> actionDelegateMock.defaultAction3())
6568
.otherwise((throwable, errorHandler) -> actionDelegateMock.defaultOtherwise())
6669
.always((throwable, errorHandler) -> actionDelegateMock.defaultAlways());
6770
}
@@ -278,6 +281,36 @@ public void testErrorHandlerBlockExecutorIgnoresNotMatchedException() {
278281
Mockito.verifyNoMoreInteractions(actionDelegateMock);
279282
}
280283

284+
@Test
285+
public void testErrorHandlerIfSkipDefaults() {
286+
InOrder testVerifier = inOrder(actionDelegateMock);
287+
288+
ErrorHandler
289+
.create()
290+
.skipDefaults()
291+
.on("closed:bar", (throwable, handler) -> {
292+
actionDelegateMock.action1();
293+
})
294+
.run(() -> {
295+
throw new BarException("", false);
296+
});
297+
298+
testVerifier.verify(actionDelegateMock).action1();
299+
Mockito.verifyNoMoreInteractions(actionDelegateMock);
300+
301+
ErrorHandler
302+
.create()
303+
.on("closed:bar", (throwable, handler) -> {
304+
actionDelegateMock.action2();
305+
})
306+
.run(() -> {
307+
throw new BarException("", false);
308+
});
309+
310+
testVerifier.verify(actionDelegateMock).action2();
311+
testVerifier.verify(actionDelegateMock).defaultAction3();
312+
}
313+
281314
private enum DBError {
282315
READ_ONLY,
283316
DEADLOCK,

0 commit comments

Comments
 (0)