From 0936dba6f577b196f537a3b6712a532a4f479288 Mon Sep 17 00:00:00 2001 From: VitaliiTihobrazov-Melsoft <79570136+VitaliiTihobrazov-Melsoft@users.noreply.github.com> Date: Fri, 26 Feb 2021 11:34:33 +0300 Subject: [PATCH] Added status code and its description () for Google API call errors (#34) If the API call throws an ApiException: - "error_status" will be set to apiException.getStatusCode() - in "error", a description of the status code will be added --- gpgs/api/gpgs.script_api | 2 + gpgs/src/java/com/defold/gpgs/GpgsJNI.java | 80 +++++++++++----------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/gpgs/api/gpgs.script_api b/gpgs/api/gpgs.script_api index f5c7099..87c7b2e 100644 --- a/gpgs/api/gpgs.script_api +++ b/gpgs/api/gpgs.script_api @@ -282,6 +282,8 @@ - `gpgs.ERROR_STATUS_SNAPSHOT_NOT_FOUND` + Or it can be ApiException.getStatusCode() (if ApiException was thrown) + - name: metadata type: table optional: true diff --git a/gpgs/src/java/com/defold/gpgs/GpgsJNI.java b/gpgs/src/java/com/defold/gpgs/GpgsJNI.java index 8e1d01c..e75dd36 100644 --- a/gpgs/src/java/com/defold/gpgs/GpgsJNI.java +++ b/gpgs/src/java/com/defold/gpgs/GpgsJNI.java @@ -41,6 +41,7 @@ import com.google.android.gms.games.AchievementsClient; import com.google.android.gms.games.achievement.Achievement; import com.google.android.gms.games.achievement.AchievementBuffer; +import com.google.android.gms.games.GamesClientStatusCodes; import com.google.android.gms.games.EventsClient; import com.google.android.gms.games.event.Event; @@ -116,9 +117,7 @@ private OnFailureListener newOnFailureListener(final int messageId, final String return new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { - sendSimpleMessage(messageId, - "status", STATUS_FAILED, - "error", message); + sendFailedMessage(messageId, message, e); } }; } @@ -171,6 +170,32 @@ private void sendSimpleMessage(int msg, String key_1, int value_1, String key_2, gpgsAddToQueue(msg, message); } + private void sendFailedMessage(int msg, String error_text, Exception e) { + if(e != null) { + + if (e instanceof ApiException) { + ApiException apiException = (ApiException) e; + Integer errorStatusCode = apiException.getStatusCode(); + error_text += ": " + GamesClientStatusCodes.getStatusCodeString(errorStatusCode) +" ("+errorStatusCode.toString()+")"; + + sendSimpleMessage(msg, + "status", STATUS_FAILED, + "error_status", errorStatusCode, + "error", error_text); + } else { + error_text += ": " + e.toString(); + + sendSimpleMessage(msg, + "status", STATUS_FAILED, + "error", error_text); + } + } else { + + sendSimpleMessage(msg, + "status", STATUS_FAILED, + "error", error_text); + } + } public GpgsJNI(Activity activity, boolean is_disk_active, boolean is_request_auth_code, boolean is_request_id_token, String client_id) { this.activity = activity; @@ -242,8 +267,7 @@ public void activityResult(int requestCode, int resultCode, Intent intent) { if (task.isSuccessful()) { onConnected(task.getResult(), MSG_SIGN_IN); } else { - sendSimpleMessage(MSG_SIGN_IN, "status", STATUS_FAILED, - "error", "Sign-in failed"); + sendFailedMessage(MSG_SIGN_IN, "Sign-in failed", task.getException()); } } else { sendSimpleMessage(MSG_SIGN_IN, "status", STATUS_FAILED, @@ -279,10 +303,7 @@ public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { onConnected(task.getResult(), MSG_SILENT_SIGN_IN); } else { - sendSimpleMessage(MSG_SILENT_SIGN_IN, - "status", STATUS_FAILED, - "error", - "Silent sign-in failed"); + sendFailedMessage(MSG_SILENT_SIGN_IN, "Silent sign-in failed", task.getException()); } } }); @@ -431,18 +452,7 @@ private OnCompleteListener getOnLoadCompleteListener() { @Override public void onComplete(@NonNull Task> task) { if (!task.isSuccessful()) { - int error_status_code = 0; - Exception e = task.getException(); - if (e instanceof ApiException) { - ApiException apiException = (ApiException) e; - error_status_code = apiException.getStatusCode(); - } - sendSimpleMessage(MSG_LOAD_SNAPSHOT, - "status", STATUS_FAILED, - "error_status", error_status_code, - "error", - "Error while opening Snapshot. " + e.toString() - ); + sendFailedMessage(MSG_LOAD_SNAPSHOT, "Error while opening Snapshot", task.getException()); } else { SnapshotsClient.DataOrConflict result = task.getResult(); if (!result.isConflict()) { @@ -451,10 +461,7 @@ public void onComplete(@NonNull Task> t currentplayerSave = mPlayerSnapshot.getSnapshotContents().readFully(); sendSnapshotMetadataMessage(MSG_LOAD_SNAPSHOT, mPlayerSnapshot.getMetadata()); } catch (IOException e) { - sendSimpleMessage(MSG_LOAD_SNAPSHOT, - "status", STATUS_FAILED, - "error", - "Error while reading Snapshot." + e.toString()); + sendFailedMessage(MSG_LOAD_SNAPSHOT, "Error while reading Snapshot", e); } } else { SnapshotsClient.SnapshotConflict conflict = result.getConflict(); @@ -466,10 +473,7 @@ public void onComplete(@NonNull Task> t sendConflictMessage(MSG_LOAD_SNAPSHOT, mPlayerSnapshot.getMetadata(), mConflictingSnapshot.getMetadata(), conflict.getConflictId()); } catch (IOException e) { - sendSimpleMessage(MSG_LOAD_SNAPSHOT, - "status", STATUS_FAILED, - "error", - "Error while reading Snapshot or Conflict." + e.toString()); + sendFailedMessage(MSG_LOAD_SNAPSHOT, "Error while reading Snapshot or Conflict", e); } } } @@ -493,7 +497,7 @@ public void showSavedGamesUI(String popupTitle, boolean allowAddButton, intentTask .addOnSuccessListener(newOnSuccessListenerForIntent(RC_LIST_SAVED_GAMES)) - .addOnFailureListener(newOnFailureListener(MSG_SHOW_SNAPSHOTS, "Can't start activity for showing saved games.")); + .addOnFailureListener(newOnFailureListener(MSG_SHOW_SNAPSHOTS, "Can't start activity for showing saved games")); } public void loadSnapshot(String saveName, boolean createIfNotFound, int conflictPolicy) { @@ -545,11 +549,7 @@ public void onComplete(@NonNull Task task) { sendSimpleMessage(MSG_SAVE_SNAPSHOT, "status", STATUS_SUCCESS); } else { - Exception e = task.getException(); - sendSimpleMessage(MSG_SAVE_SNAPSHOT, - "status", STATUS_FAILED, - "error", - "Failed to save a snapshot. " + e.toString()); + sendFailedMessage(MSG_SAVE_SNAPSHOT, "Failed to save a snapshot", task.getException()); } } }); @@ -664,7 +664,7 @@ public void onSuccess(AnnotatedData data) gpgsAddToQueue(MSG_GET_TOP_SCORES, message); } }) - .addOnFailureListener(newOnFailureListener(MSG_GET_TOP_SCORES, "Unable to get top scores.")); + .addOnFailureListener(newOnFailureListener(MSG_GET_TOP_SCORES, "Unable to get top scores")); } } @@ -691,7 +691,7 @@ public void onSuccess(AnnotatedData data) gpgsAddToQueue(MSG_GET_PLAYER_CENTERED_SCORES, message); } }) - .addOnFailureListener(newOnFailureListener(MSG_GET_PLAYER_CENTERED_SCORES, "Unable to get player centered scores.")); + .addOnFailureListener(newOnFailureListener(MSG_GET_PLAYER_CENTERED_SCORES, "Unable to get player centered scores")); } } @@ -718,7 +718,7 @@ public void onSuccess(AnnotatedData data) { gpgsAddToQueue(MSG_GET_PLAYER_SCORE, message); } }) - .addOnFailureListener(newOnFailureListener(MSG_GET_PLAYER_SCORE, "Unable to get player scores.")); + .addOnFailureListener(newOnFailureListener(MSG_GET_PLAYER_SCORE, "Unable to get player scores")); } } @@ -822,7 +822,7 @@ else if (a.getState() == Achievement.STATE_REVEALED) { gpgsAddToQueue(MSG_ACHIEVEMENTS, message); } }) - .addOnFailureListener(newOnFailureListener(MSG_ACHIEVEMENTS, "Unable to get achievements.")); + .addOnFailureListener(newOnFailureListener(MSG_ACHIEVEMENTS, "Unable to get achievements")); } } @@ -873,7 +873,7 @@ public void onSuccess(AnnotatedData data) { gpgsAddToQueue(MSG_GET_EVENTS, message); } }) - .addOnFailureListener(newOnFailureListener(MSG_GET_EVENTS, "Unable to get events.")); + .addOnFailureListener(newOnFailureListener(MSG_GET_EVENTS, "Unable to get events")); } }