Skip to content

Commit

Permalink
Added status code and its description () for Google API call errors (#34
Browse files Browse the repository at this point in the history
)

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
  • Loading branch information
vtihobrazov-ms committed Feb 26, 2021
1 parent 1d191bc commit 0936dba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
2 changes: 2 additions & 0 deletions gpgs/api/gpgs.script_api
Expand Up @@ -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
Expand Down
80 changes: 40 additions & 40 deletions gpgs/src/java/com/defold/gpgs/GpgsJNI.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
};
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -279,10 +303,7 @@ public void onComplete(@NonNull Task<GoogleSignInAccount> 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());
}
}
});
Expand Down Expand Up @@ -431,18 +452,7 @@ private OnCompleteListener getOnLoadCompleteListener() {
@Override
public void onComplete(@NonNull Task<SnapshotsClient.DataOrConflict<Snapshot>> 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<Snapshot> result = task.getResult();
if (!result.isConflict()) {
Expand All @@ -451,10 +461,7 @@ public void onComplete(@NonNull Task<SnapshotsClient.DataOrConflict<Snapshot>> 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();
Expand All @@ -466,10 +473,7 @@ public void onComplete(@NonNull Task<SnapshotsClient.DataOrConflict<Snapshot>> 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);
}
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -545,11 +549,7 @@ public void onComplete(@NonNull Task<SnapshotMetadata> 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());
}
}
});
Expand Down Expand Up @@ -664,7 +664,7 @@ public void onSuccess(AnnotatedData<LeaderboardsClient.LeaderboardScores> 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"));
}
}

Expand All @@ -691,7 +691,7 @@ public void onSuccess(AnnotatedData<LeaderboardsClient.LeaderboardScores> 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"));
}
}

Expand All @@ -718,7 +718,7 @@ public void onSuccess(AnnotatedData<LeaderboardScore> 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"));
}
}

Expand Down Expand Up @@ -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"));
}
}

Expand Down Expand Up @@ -873,7 +873,7 @@ public void onSuccess(AnnotatedData<EventBuffer> data) {
gpgsAddToQueue(MSG_GET_EVENTS, message);
}
})
.addOnFailureListener(newOnFailureListener(MSG_GET_EVENTS, "Unable to get events."));
.addOnFailureListener(newOnFailureListener(MSG_GET_EVENTS, "Unable to get events"));
}
}

Expand Down

0 comments on commit 0936dba

Please sign in to comment.