Skip to content

Commit d6b1125

Browse files
committed
add tests for reading server error messages
1 parent c0845f6 commit d6b1125

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

intercom-java/src/main/java/io/intercom/api/IntercomException.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ public IntercomException(ErrorCollection errorCollection) {
2121
}
2222

2323
public static String getMessage(ErrorCollection errorCollection) {
24-
String message = "";
25-
if(errorCollection!=null && errorCollection.getErrors() != null && errorCollection.getErrors().size() > 0 && errorCollection.getErrors().get(0) != null){
24+
String message = "Could not read error message from server";
25+
if(errorCollection!=null
26+
&& errorCollection.getErrors() != null
27+
&& errorCollection.getErrors().size() > 0
28+
&& errorCollection.getErrors().get(0) != null
29+
&& errorCollection.getErrors().get(0).getMessage() != null){
2630
message = errorCollection.getErrors().get(0).getMessage();
2731
}
2832
return message;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.intercom.api;
2+
3+
import com.google.common.collect.Lists;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class IntercomExceptionTest {
9+
10+
@Test
11+
public void testNullCollection() {
12+
assertEquals("Could not read error message from server", IntercomException.getMessage(null));
13+
}
14+
15+
@Test
16+
public void testBlankCollection() {
17+
ErrorCollection ec = new ErrorCollection();
18+
assertEquals("Could not read error message from server", IntercomException.getMessage(ec));
19+
}
20+
21+
@Test
22+
public void testEmptyCollection() {
23+
try {
24+
new ErrorCollection(Lists.<Error>newArrayList());
25+
fail();
26+
} catch (IllegalArgumentException e) {
27+
}
28+
final ErrorCollection ec = new ErrorCollection(Lists.newArrayList(new Error()));
29+
assertEquals("Could not read error message from server", IntercomException.getMessage(ec));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)