Yuvipanda has submitted this change and it was merged.

Change subject: Wrap IOException when it happens with ApiException
......................................................................


Wrap IOException when it happens with ApiException

Change-Id: Ie47c8d6cb40a44d0e38ed264b9d14f282a95752a
---
M src/main/java/org/mediawiki/api/Api.java
M src/test/java/org/mediawiki/api/ApiTest.java
2 files changed, 24 insertions(+), 0 deletions(-)

Approvals:
  Yuvipanda: Verified; Looks good to me, approved



diff --git a/src/main/java/org/mediawiki/api/Api.java 
b/src/main/java/org/mediawiki/api/Api.java
index 918de1f..4380e35 100644
--- a/src/main/java/org/mediawiki/api/Api.java
+++ b/src/main/java/org/mediawiki/api/Api.java
@@ -124,6 +124,8 @@
             return new JSONObject(request.body());
         } catch (JSONException e) {
             throw new ApiException(e);
+        } catch (HttpRequest.HttpRequestException e) {
+            throw new ApiException(e.getCause());
         }
     }
 }
diff --git a/src/test/java/org/mediawiki/api/ApiTest.java 
b/src/test/java/org/mediawiki/api/ApiTest.java
index 057fbd2..f186c32 100644
--- a/src/test/java/org/mediawiki/api/ApiTest.java
+++ b/src/test/java/org/mediawiki/api/ApiTest.java
@@ -6,6 +6,8 @@
 import org.json.JSONObject;
 import org.junit.*;
 
+import java.io.IOException;
+
 /**
  * Tests that actually hit the API to return something.
  */
@@ -74,4 +76,24 @@
         }
         assertTrue("This means no exception was thrown, and hence test fails", 
false);
     }
+
+    /**
+     * Test for Network failure throwing the proper exception.
+     *
+     * Usually this happens due to various network failures, but since we can 
not
+     * reproduce that reliably without network mocks, I am simply calling into 
a
+     * random hopefully nonexistant hostname to get the network failure.
+     */
+    @Test
+    public void testNetworkException() {
+        Api api = new Api("blashblahblahdoesnotexist");
+        try {
+            api.action("somethingdoesnmtatter").param("format", "xml").get();
+        } catch (ApiException e) {
+            assertTrue(e.getCause() instanceof IOException);
+            return;
+        }
+        assertTrue("This means no exception was thrown, and hence test fails", 
false);
+    }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/92478
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie47c8d6cb40a44d0e38ed264b9d14f282a95752a
Gerrit-PatchSet: 2
Gerrit-Project: apps/android/java-mwapi
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: Yuvipanda <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to