mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Handle response errors through Util.getCodeFromResponse
Centralizing the exceptions to provide more details on fail.
This commit is contained in:
parent
8648f8bde7
commit
3970fb146c
3 changed files with 36 additions and 11 deletions
|
@ -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.push.exception.UnexpectedPostbackException;
|
||||
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostExceptionBuilder;
|
||||
import uk.ac.ic.wlgitbridge.util.Util;
|
||||
|
||||
/**
|
||||
* Created by Winston on 17/11/14.
|
||||
|
@ -36,7 +37,7 @@ public class PostbackContents implements JSONSource {
|
|||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
JsonObject responseObject = json.getAsJsonObject();
|
||||
String code = responseObject.get("code").getAsString();
|
||||
String code = Util.getCodeFromResponse(responseObject);
|
||||
setResult(responseObject, code);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,16 +29,9 @@ public class PushResult extends Result {
|
|||
|
||||
@Override
|
||||
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")) {
|
||||
success = true;
|
||||
} else if (code.equals("outOfDate")) {
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package uk.ac.ic.wlgitbridge.util;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.*;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
@ -190,4 +193,32 @@ public class Util {
|
|||
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 (codeElement == null) {
|
||||
code = statusElement.getAsString();
|
||||
if (code.equals("422")) {
|
||||
error = "Unprocessable entity";
|
||||
} else if (code.equals("404")) {
|
||||
error = "Not found";
|
||||
} else if (code.equals("403")) {
|
||||
error = "Forbidden";
|
||||
}
|
||||
}
|
||||
throw new RuntimeException(error);
|
||||
} else {
|
||||
code = codeElement.getAsString();
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue