Skip to content

Commit

Permalink
use default exception handler if custom handler fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvard Fonsell committed Feb 6, 2021
1 parent ba97b54 commit 94ea98e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.nflow.engine.exception;

import static org.slf4j.LoggerFactory.getLogger;

import org.slf4j.Logger;
import org.slf4j.event.Level;
import org.springframework.stereotype.Component;

Expand All @@ -14,14 +17,36 @@
@Component
public class DispatcherExceptionAnalyzer {

private static final Logger logger = getLogger(DispatcherExceptionAnalyzer.class);

/**
* Analyze the exception.
*
* @param e
* The exception to be analyzed.
* @return How the exception should be handled.
*/
public DispatcherExceptionHandling analyze(Exception e) {
public final DispatcherExceptionHandling analyzeSafely(Exception e) {
try {
return analyze(e);
} catch (Exception analyzerException) {
logger.error("Failed to analyze exception, using default handling.", analyzerException);
}
return getDefultHandling(e);
}

/**
* Override this to provide custom handling.
*
* @param e
* The exception to be analyzed.
* @return How the exception should be handled.
*/
protected DispatcherExceptionHandling analyze(Exception e) {
return getDefultHandling(e);
}

private DispatcherExceptionHandling getDefultHandling(Exception e) {
Builder builder = new DispatcherExceptionHandling.Builder();
if (e instanceof PollingRaceConditionException) {
builder.setLogLevel(Level.DEBUG).setLogStackTrace(false).setRandomizeSleep(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void run() {
dispatch(getNextInstanceIds());
}
} catch (Exception e) {
DispatcherExceptionHandling handling = exceptionAnalyzer.analyze(e);
DispatcherExceptionHandling handling = exceptionAnalyzer.analyzeSafely(e);
if (handling.log) {
if (handling.logStackTrace) {
StringBuilder sb = new StringBuilder("Exception in executing dispatcher - retrying");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DispatcherExceptionAnalyzerTest {

@Test
void analyzeGenericExceptionReturnsDefaults() {
DispatcherExceptionHandling handling = analyzer.analyze(new Exception());
DispatcherExceptionHandling handling = analyzer.analyzeSafely(new Exception());

assertTrue(handling.log);
assertEquals(handling.logLevel, Level.ERROR);
Expand All @@ -27,7 +27,7 @@ void analyzeGenericExceptionReturnsDefaults() {

@Test
void analyzePollingRaceConditionException() {
DispatcherExceptionHandling handling = analyzer.analyze(new PollingRaceConditionException("error"));
DispatcherExceptionHandling handling = analyzer.analyzeSafely(new PollingRaceConditionException("error"));

assertTrue(handling.log);
assertEquals(handling.logLevel, Level.DEBUG);
Expand All @@ -38,7 +38,7 @@ void analyzePollingRaceConditionException() {

@Test
void analyzePollingBatchException() {
DispatcherExceptionHandling handling = analyzer.analyze(new PollingBatchException("error"));
DispatcherExceptionHandling handling = analyzer.analyzeSafely(new PollingBatchException("error"));

assertTrue(handling.log);
assertEquals(handling.logLevel, Level.WARN);
Expand All @@ -48,9 +48,41 @@ void analyzePollingBatchException() {

@Test
void analyzeInterruptedException() {
DispatcherExceptionHandling handling = analyzer.analyze(new InterruptedException());
DispatcherExceptionHandling handling = analyzer.analyzeSafely(new InterruptedException());

assertFalse(handling.log);
assertFalse(handling.sleep);
}

@Test
void customAnalyzerCanBeUsed() {
DispatcherExceptionAnalyzer customAnalyzer = new DispatcherExceptionAnalyzer() {
@Override
protected DispatcherExceptionHandling analyze(Exception e) {
return new DispatcherExceptionHandling.Builder().setLogStackTrace(false).build();
}
};

DispatcherExceptionHandling handling = customAnalyzer.analyzeSafely(new Exception());

assertFalse(handling.logStackTrace);
}

@Test
void defaultAnalyzerIsUsedWhenCustomerAnalyzerFails() {
DispatcherExceptionAnalyzer customAnalyzer = new DispatcherExceptionAnalyzer() {
@Override
protected DispatcherExceptionHandling analyze(Exception e) {
throw new IllegalStateException("fail");
}
};

DispatcherExceptionHandling handling = customAnalyzer.analyzeSafely(new Exception());

assertTrue(handling.log);
assertEquals(handling.logLevel, Level.ERROR);
assertTrue(handling.logStackTrace);
assertTrue(handling.sleep);
assertFalse(handling.randomizeSleep);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void defaultAnalyzerReturnsConfiguredDelay() {
}

@Test
void customerAnalyzerCanBeUsed() {
void customAnalyzerCanBeUsed() {
StateSaveExceptionAnalyzer analyzer = new StateSaveExceptionAnalyzer(env) {
@Override
protected StateSaveExceptionHandling analyze(Exception e, int saveRetryCount) {
Expand Down

0 comments on commit 94ea98e

Please sign in to comment.