Yuvipanda has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/92477


Change subject: Create wrapper exception to wrap around JSONException
......................................................................

Create wrapper exception to wrap around JSONException

Since most of the time we will be handling the various exceptions
that can pop up in the same way ('operation failed, please
try again'), and the actual errors returned by MediaWiki will
be in the JSON structure itself and need to be handled separately,
it makes sense to have a single wrapper exception to reduce the
number of catch blocks absolutely needed. Most client code
can probably treat the two wrapped exceptions the same and not
lose much.

Wrapping the other exception (IOException) will be done in next
commit.

Change-Id: Id74be171dffd670277e0b49a492e3813ceed983a
---
M src/main/java/org/mediawiki/api/Api.java
M src/main/java/org/mediawiki/api/RequestBuilder.java
M src/test/java/org/mediawiki/api/ApiConstructionTest.java
M src/test/java/org/mediawiki/api/ApiTest.java
4 files changed, 40 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/java-mwapi 
refs/changes/77/92477/1

diff --git a/src/main/java/org/mediawiki/api/Api.java 
b/src/main/java/org/mediawiki/api/Api.java
index a39537e..918de1f 100644
--- a/src/main/java/org/mediawiki/api/Api.java
+++ b/src/main/java/org/mediawiki/api/Api.java
@@ -105,9 +105,10 @@
      *
      * @param method HTTP method to use when performing the request
      * @param requestBuilder The requestBuilder to use to construct the request
+     * @throws ApiException In case of NetworkError or JSON parsing error
      * @return A JSONObject with the response from the server
      */
-    public JSONObject makeRequest(final int method, final RequestBuilder 
requestBuilder) {
+    public JSONObject makeRequest(final int method, final RequestBuilder 
requestBuilder) throws ApiException {
         HttpRequest request;
         switch(method) {
             case METHOD_GET:
@@ -122,8 +123,7 @@
         try {
             return new JSONObject(request.body());
         } catch (JSONException e) {
-            // Well, if the request *does* complete successfully but the
-            throw new RuntimeException(e);
+            throw new ApiException(e);
         }
     }
 }
diff --git a/src/main/java/org/mediawiki/api/RequestBuilder.java 
b/src/main/java/org/mediawiki/api/RequestBuilder.java
index a338ac2..ab1b817 100644
--- a/src/main/java/org/mediawiki/api/RequestBuilder.java
+++ b/src/main/java/org/mediawiki/api/RequestBuilder.java
@@ -54,27 +54,30 @@
      * Performs the request that has been built up so far and returns the JSON 
Object.
      *
      * @param method HTTP Method to use when performing the request
+     * @throws ApiException In case of NetworkError or JSON parsing error
      * @return The result of the API request
      */
-    private JSONObject makeRequest(final int method) {
+    private JSONObject makeRequest(final int method) throws ApiException {
         return api.makeRequest(method, this);
     }
 
     /**
      * Performs a GET request using the parameters so far specified.
      *
+     * @throws ApiException In case of NetworkError or JSON parsing error
      * @return The result of the API request
      */
-    public JSONObject get() {
+    public JSONObject get() throws ApiException {
         return makeRequest(Api.METHOD_GET);
     }
 
     /**
      * Performs a POST request using the parameters so far specified.
      *
+     * @throws ApiException In case of NetworkError or JSON parsing error
      * @return The result of the API request
      */
-    public JSONObject post() {
+    public JSONObject post() throws ApiException {
         return makeRequest(Api.METHOD_POST);
     }
 }
diff --git a/src/test/java/org/mediawiki/api/ApiConstructionTest.java 
b/src/test/java/org/mediawiki/api/ApiConstructionTest.java
index fe29d8e..2cf3ced 100644
--- a/src/test/java/org/mediawiki/api/ApiConstructionTest.java
+++ b/src/test/java/org/mediawiki/api/ApiConstructionTest.java
@@ -39,4 +39,11 @@
                 "https://test.wikipedia.org/w/api.php";
                 );
     }
+
+    @Test(expected = RuntimeException.class)
+    public void testInvalidApiException() {
+        new ApiException(new RuntimeException());
+    }
+
+
 }
diff --git a/src/test/java/org/mediawiki/api/ApiTest.java 
b/src/test/java/org/mediawiki/api/ApiTest.java
index 1736d68..b41e781 100644
--- a/src/test/java/org/mediawiki/api/ApiTest.java
+++ b/src/test/java/org/mediawiki/api/ApiTest.java
@@ -2,6 +2,7 @@
 
 import static org.junit.Assert.*;
 
+import org.json.JSONException;
 import org.json.JSONObject;
 import org.junit.*;
 
@@ -11,7 +12,7 @@
 public class ApiTest {
 
     @Test
-    public void testBasicPost() {
+    public void testBasicPost() throws Exception {
         Api api = new Api("test.wikipedia.org");
         // We're just checking if the POST goes through, so does not
         // matter which username password we use
@@ -27,7 +28,7 @@
     }
 
     @Test
-    public void testWrongMethod() {
+    public void testWrongMethod() throws Exception {
         Api api = new Api("test.wikipedia.org");
         JSONObject resp = api.action("login")
                 .get();
@@ -49,8 +50,28 @@
     }
 
     @Test(expected = IllegalArgumentException.class)
-    public void testInvalidMethod() {
+    public void testInvalidMethod() throws Exception {
         Api api = new Api("test.wikipedia.org");
         api.makeRequest(404, null);
     }
+
+    /**
+     * This tests for responses that aren't JSON, but something else.
+     *
+     * Usually this happens when a backend apache times out and we get the
+     * timeout HTML page from the frontend caches. Since that is not
+     * reliably reproducible, and we aren't using network mocks, I can
+     * simulate it by simply requesting for the XML format.
+     */
+    @Test
+    public void testJSONException() {
+        Api api = new Api("test.wikipedia.org");
+        try {
+            api.action("somethingdoesnmtatter").param("format", "xml").get();
+        } catch (ApiException e) {
+            assertEquals(e.getCause().getClass(), JSONException.class);
+            return;
+        }
+        assertTrue("This means no exception was thrown, and hence test fails", 
false);
+    }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id74be171dffd670277e0b49a492e3813ceed983a
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/java-mwapi
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <[email protected]>

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

Reply via email to