Skip to content
This repository has been archived by the owner on May 3, 2020. It is now read-only.

Commit

Permalink
Include original exception when calling getValue
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Mar 28, 2013
1 parent 4e05562 commit 8336f0a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import com.google.common.base.Charsets;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.io.CharStreams;
Expand All @@ -27,6 +25,7 @@
import io.airlift.json.JsonCodec;

import javax.annotation.concurrent.NotThreadSafe;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
Expand Down Expand Up @@ -151,7 +150,9 @@ public boolean hasValue()

public T getValue()
{
Preconditions.checkState(hasValue, "Response does not contain a JSON value");
if (!hasValue) {
throw new IllegalStateException("Response does not contain a JSON value", exception);
}
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public class TestFullJsonResponseHandler
Expand Down Expand Up @@ -57,6 +58,22 @@ public void testInvalidJson()
"Invalid [simple type, class io.airlift.http.client.TestFullJsonResponseHandler$User] json string");
}

@Test
public void testInvalidJsonGetValue()
{
String json = "{\"age\": \"foo\"}";
JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json));

try {
response.getValue();
fail("expected exception");
}
catch (IllegalStateException e) {
assertEquals(e.getMessage(), "Response does not contain a JSON value");
assertEquals(e.getCause(), response.getException());
}
}

@Test
public void testNonJsonResponse()
{
Expand Down

0 comments on commit 8336f0a

Please sign in to comment.