Skip to content

Commit 671c5db

Browse files
committed
Fix fetching message from caused throwable
1 parent d1096af commit 671c5db

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/main/java/pl/wavesoftware/eid/exceptions/EidRuntimeException.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package pl.wavesoftware.eid.exceptions;
1717

18+
import javax.annotation.Nullable;
19+
1820
/**
1921
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
2022
* of end user applications which will report Bugs in standardized error pages or post them to issue tracker.
@@ -118,7 +120,17 @@ public Class<? extends RuntimeException> getStandardJdkClass() {
118120
}
119121

120122
private static String message(Throwable cause) {
121-
return cause.getLocalizedMessage();
123+
String msg = coalesce(cause.getLocalizedMessage(), cause.getMessage());
124+
return coalesce(msg, cause.toString());
125+
}
126+
127+
@Nullable
128+
private static <T> T coalesce(@Nullable T first, @Nullable T second) {
129+
if (first == null) {
130+
return second;
131+
} else {
132+
return first;
133+
}
122134
}
123135

124136
}

src/test/java/pl/wavesoftware/eid/exceptions/ExceptionsTest.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515
*/
1616
package pl.wavesoftware.eid.exceptions;
1717

18-
import java.lang.reflect.Constructor;
19-
import java.lang.reflect.InvocationTargetException;
20-
import java.util.List;
21-
import java.util.Map;
22-
import static org.assertj.core.api.Assertions.assertThat;
2318
import org.assertj.core.util.Lists;
2419
import org.assertj.core.util.Maps;
2520
import org.junit.Test;
2621
import org.junit.runner.RunWith;
2722
import org.junit.runners.Parameterized;
2823
import org.junit.runners.Parameterized.Parameters;
2924

25+
import java.lang.reflect.Constructor;
26+
import java.lang.reflect.InvocationTargetException;
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
3032
/**
3133
*
3234
* @author Krzysztof Suszyński <[email protected]>
@@ -47,7 +49,7 @@ private static Map<Class<? extends EidRuntimeException>, Class<? extends Runtime
4749
}
4850

4951
private static List<Object[]> getArguments() {
50-
Throwable cause = new InterruptedException();
52+
Throwable cause = new InterruptedException("A testing message");
5153
String eid = "20150718:112954";
5254
String ref = "PL-981";
5355
Eid id = new Eid(eid);
@@ -148,10 +150,24 @@ public void testGetStandardJdkClass() {
148150
assertThat(jdkCls).isEqualTo(jdkClass);
149151
}
150152

153+
@Test
154+
public void testMessage() {
155+
// given
156+
EidRuntimeException exception = construct();
157+
boolean hasCause = exception.getCause() != null;
158+
159+
// then
160+
assertThat(exception).hasMessageContaining("20150718:112954");
161+
if (hasCause) {
162+
assertThat(exception).hasMessageContaining("A testing message");
163+
}
164+
}
165+
151166
@Test
152167
public void testConstruction() {
153168
// given
154169
EidRuntimeException exception = construct();
170+
155171
// then
156172
assertThat(exception).isNotNull();
157173
assertThat(exception).isExactlyInstanceOf(eidClass);

0 commit comments

Comments
 (0)