Skip to content

Commit 2953ed1

Browse files
henri-tremblaywilkinsona
authored andcommitted
Unwrap InvocationTargetException in isLogConfigurationMessage
See gh-12958
1 parent 34af023 commit 2953ed1

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

spring-boot/src/main/java/org/springframework/boot/SpringBootExceptionHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ private boolean isPassedToParent(Throwable ex) {
8686
* @return {@code true} if the exception contains a log configuration message
8787
*/
8888
private boolean isLogConfigurationMessage(Throwable ex) {
89+
if (ex instanceof InvocationTargetException) {
90+
return isLogConfigurationMessage(ex.getCause());
91+
}
92+
8993
String message = ex.getMessage();
9094
if (message != null) {
9195
for (String candidate : LOG_CONFIGURATION_MESSAGES) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import java.lang.reflect.InvocationTargetException;
20+
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.mockito.InjectMocks;
24+
import org.mockito.Mock;
25+
import org.mockito.junit.MockitoJUnit;
26+
import org.mockito.junit.MockitoRule;
27+
28+
29+
import static org.mockito.ArgumentMatchers.same;
30+
import static org.mockito.Mockito.verify;
31+
import static org.mockito.Mockito.verifyZeroInteractions;
32+
33+
/**
34+
* Tests for {@link SpringBootExceptionHandler}.
35+
*
36+
* @author Henri Tremblay
37+
*/
38+
public class SpringBootExceptionHandlerTest {
39+
40+
@Rule
41+
public MockitoRule rule = MockitoJUnit.rule();
42+
43+
@Mock
44+
private Thread.UncaughtExceptionHandler parent;
45+
46+
@InjectMocks
47+
private SpringBootExceptionHandler handler;
48+
49+
@Test
50+
public void uncaughtException_shouldNotForwardLoggedErrorToParent() {
51+
Thread thread = Thread.currentThread();
52+
Exception ex = new Exception();
53+
this.handler.registerLoggedException(ex);
54+
55+
this.handler.uncaughtException(thread, ex);
56+
57+
verifyZeroInteractions(this.parent);
58+
}
59+
60+
@Test
61+
public void uncaughtException_shouldForwardLogConfigurationErrorToParent() {
62+
Thread thread = Thread.currentThread();
63+
Exception ex = new Exception("[stuff] Logback configuration error detected [stuff]");
64+
this.handler.registerLoggedException(ex);
65+
66+
this.handler.uncaughtException(thread, ex);
67+
68+
verify(this.parent).uncaughtException(same(thread), same(ex));
69+
}
70+
71+
@Test
72+
public void uncaughtException_shouldForwardLogConfigurationErrorToParentEvenWhenWrapped() {
73+
Thread thread = Thread.currentThread();
74+
Exception ex = new InvocationTargetException(new Exception("[stuff] Logback configuration error detected [stuff]", new Exception()));
75+
this.handler.registerLoggedException(ex);
76+
77+
this.handler.uncaughtException(thread, ex);
78+
79+
verify(this.parent).uncaughtException(same(thread), same(ex));
80+
}
81+
}

0 commit comments

Comments
 (0)