Skip to content

Commit bf642ff

Browse files
committed
Continue failure analysis when an analyzer throws an exception
Previously, if a failure analyzer threw an exception from its analyze method, failure analysis would stop. This commit updates FailureAnalyzers to catch and log any Throwable thrown by an analyzer and continue to the next available analyzer. Closes gh-7956
1 parent 7298b2d commit bf642ff

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ public boolean analyzeAndReport(Throwable failure) {
115115

116116
private FailureAnalysis analyze(Throwable failure, List<FailureAnalyzer> analyzers) {
117117
for (FailureAnalyzer analyzer : analyzers) {
118-
FailureAnalysis analysis = analyzer.analyze(failure);
119-
if (analysis != null) {
120-
return analysis;
118+
try {
119+
FailureAnalysis analysis = analyzer.analyze(failure);
120+
if (analysis != null) {
121+
return analysis;
122+
}
123+
}
124+
catch (Throwable ex) {
125+
log.debug("FailureAnalyzer " + analyzer + " failed", ex);
121126
}
122127
}
123128
return null;

spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,16 @@ public void beanFactoryIsInjectedIntoBeanFactoryAwareFailureAnalyzers() {
6262
}
6363

6464
@Test
65-
public void brokenAnalyzerDoesNotPreventOtherAnalyzersFromBeingCalled() {
65+
public void analyzerThatFailsDuringInitializationDoesNotPreventOtherAnalyzersFromBeingCalled() {
6666
RuntimeException failure = new RuntimeException();
67-
analyzeAndReport("broken.factories", failure);
67+
analyzeAndReport("broken-initialization.factories", failure);
68+
verify(failureAnalyzer, times(1)).analyze(failure);
69+
}
70+
71+
@Test
72+
public void analyzerThatFailsDuringAnalysisDoesNotPreventOtherAnalyzersFromBeingCalled() {
73+
RuntimeException failure = new RuntimeException();
74+
analyzeAndReport("broken-analysis.factories", failure);
6875
verify(failureAnalyzer, times(1)).analyze(failure);
6976
}
7077

@@ -83,7 +90,7 @@ public FailureAnalysis analyze(Throwable failure) {
8390

8491
}
8592

86-
static class BrokenFailureAnalyzer implements FailureAnalyzer {
93+
static class BrokenInitializationFailureAnalyzer implements FailureAnalyzer {
8794

8895
static {
8996
Object foo = null;
@@ -97,6 +104,15 @@ public FailureAnalysis analyze(Throwable failure) {
97104

98105
}
99106

107+
static class BrokenAnalysisFailureAnalyzer implements FailureAnalyzer {
108+
109+
@Override
110+
public FailureAnalysis analyze(Throwable failure) {
111+
throw new NoClassDefFoundError();
112+
}
113+
114+
}
115+
100116
interface BeanFactoryAwareFailureAnalyzer extends BeanFactoryAware, FailureAnalyzer {
101117

102118
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Failure Analyzers
22
org.springframework.boot.diagnostics.FailureAnalyzer=\
3-
org.springframework.boot.diagnostics.FailureAnalyzersTests$BrokenFailureAnalyzer,\
3+
org.springframework.boot.diagnostics.FailureAnalyzersTests$BrokenAnalysisFailureAnalyzer,\
44
org.springframework.boot.diagnostics.FailureAnalyzersTests$BasicFailureAnalyzer
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Failure Analyzers
2+
org.springframework.boot.diagnostics.FailureAnalyzer=\
3+
org.springframework.boot.diagnostics.FailureAnalyzersTests$BrokenAnalysisFailureAnalyzer,\
4+
org.springframework.boot.diagnostics.FailureAnalyzersTests$BasicFailureAnalyzer

0 commit comments

Comments
 (0)