Skip to content

Commit

Permalink
Fix fetching message from caused throwable
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Oct 7, 2015
1 parent d1096af commit 671c5db
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package pl.wavesoftware.eid.exceptions;

import javax.annotation.Nullable;

/**
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
* of end user applications which will report Bugs in standardized error pages or post them to issue tracker.
Expand Down Expand Up @@ -118,7 +120,17 @@ public Class<? extends RuntimeException> getStandardJdkClass() {
}

private static String message(Throwable cause) {
return cause.getLocalizedMessage();
String msg = coalesce(cause.getLocalizedMessage(), cause.getMessage());
return coalesce(msg, cause.toString());
}

@Nullable
private static <T> T coalesce(@Nullable T first, @Nullable T second) {
if (first == null) {
return second;
} else {
return first;
}
}

}
28 changes: 22 additions & 6 deletions src/test/java/pl/wavesoftware/eid/exceptions/ExceptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
*/
package pl.wavesoftware.eid.exceptions;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.util.Lists;
import org.assertj.core.util.Maps;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/**
*
* @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl>
Expand All @@ -47,7 +49,7 @@ private static Map<Class<? extends EidRuntimeException>, Class<? extends Runtime
}

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

@Test
public void testMessage() {
// given
EidRuntimeException exception = construct();
boolean hasCause = exception.getCause() != null;

// then
assertThat(exception).hasMessageContaining("20150718:112954");
if (hasCause) {
assertThat(exception).hasMessageContaining("A testing message");
}
}

@Test
public void testConstruction() {
// given
EidRuntimeException exception = construct();

// then
assertThat(exception).isNotNull();
assertThat(exception).isExactlyInstanceOf(eidClass);
Expand Down

0 comments on commit 671c5db

Please sign in to comment.