Преглед изворни кода

Merge pull request #3 from overleaf/error_handling

Error handling
John Lees-Miller пре 11 година
родитељ
комит
946c1d56be

+ 2 - 1
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PostbackContents.java

@@ -8,6 +8,7 @@ import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
 import uk.ac.ic.wlgitbridge.snapshot.base.JSONSource;
 import uk.ac.ic.wlgitbridge.snapshot.base.JSONSource;
 import uk.ac.ic.wlgitbridge.snapshot.push.exception.UnexpectedPostbackException;
 import uk.ac.ic.wlgitbridge.snapshot.push.exception.UnexpectedPostbackException;
 import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostExceptionBuilder;
 import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostExceptionBuilder;
+import uk.ac.ic.wlgitbridge.util.Util;
 
 
 /**
 /**
  * Created by Winston on 17/11/14.
  * Created by Winston on 17/11/14.
@@ -36,7 +37,7 @@ public class PostbackContents implements JSONSource {
     @Override
     @Override
     public void fromJSON(JsonElement json) {
     public void fromJSON(JsonElement json) {
         JsonObject responseObject = json.getAsJsonObject();
         JsonObject responseObject = json.getAsJsonObject();
-        String code = responseObject.get("code").getAsString();
+        String code = Util.getCodeFromResponse(responseObject);
         setResult(responseObject, code);
         setResult(responseObject, code);
     }
     }
 
 

+ 3 - 10
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/push/PushResult.java

@@ -29,16 +29,9 @@ public class PushResult extends Result {
 
 
     @Override
     @Override
     public void fromJSON(JsonElement json) {
     public void fromJSON(JsonElement json) {
-        String code;
-        try {
-            JsonObject responseObject = json.getAsJsonObject();
-            code = responseObject.get("code").getAsString();
-        } catch (Exception e) {
-            Util.serr("Unexpected response from API:");
-            Util.serr(json.toString());
-            Util.serr("End of response");
-            throw e;
-        }
+        JsonObject responseObject = json.getAsJsonObject();
+        String code = Util.getCodeFromResponse(responseObject);
+
         if (code.equals("accepted")) {
         if (code.equals("accepted")) {
             success = true;
             success = true;
         } else if (code.equals("outOfDate")) {
         } else if (code.equals("outOfDate")) {

+ 31 - 0
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Util.java

@@ -1,5 +1,8 @@
 package uk.ac.ic.wlgitbridge.util;
 package uk.ac.ic.wlgitbridge.util;
 
 
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
 import java.io.*;
 import java.io.*;
 import java.text.DateFormat;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.text.SimpleDateFormat;
@@ -190,4 +193,32 @@ public class Util {
         return fromStream(stream, 0);
         return fromStream(stream, 0);
     }
     }
 
 
+    public static String getCodeFromResponse(JsonObject json) {
+        String code = "error";
+        JsonElement codeElement = json.get("code");
+
+        if (codeElement == null) {
+            String error = "Unexpected error";
+            serr("Unexpected response from API:");
+            serr(json.toString());
+            serr("End of response");
+            JsonElement statusElement = json.get("status");
+            if (statusElement != null) {
+                String status = statusElement.getAsString();
+                if (status.equals("422")) {
+                    error = "Unprocessable entity";
+                } else if (status.equals("404")) {
+                    error = "Not found";
+                } else if (status.equals("403")) {
+                    error = "Forbidden";
+                }
+            }
+            throw new RuntimeException(error);
+        } else {
+            code = codeElement.getAsString();
+        }
+
+        return code;
+    }
+
 }
 }