Skip to content

b/80200799: Automatically log warnings for errors that likely represent developer mistakes / oversights. #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions firebase-firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- [changed] Changed `get()` to only make 1 attempt to reach the backend before
returning cached data, potentially reducing delays while offline. Previously
it would make 2 attempts, to work around a backend bug.
- [changed] Some SDK errors that represent common mistakes (such as permission
denied or a missing index) will automatically be logged as a warning in
addition to being surfaced via the API.

# 17.1.0
- [feature] Added `FieldValue.arrayUnion()` and `FieldValue.arrayRemove()` to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,11 @@ public void handleRejectedListen(int targetId, Status error) {
} else {
QueryView queryView = queryViewsByTarget.get(targetId);
hardAssert(queryView != null, "Unknown target: %s", targetId);
localStore.releaseQuery(queryView.getQuery());
Query query = queryView.getQuery();
localStore.releaseQuery(query);
removeAndCleanup(queryView);
callback.onError(queryView.getQuery(), error);
logErrorIfInteresting(error, "Listen for %s failed", query);
callback.onError(query, error);
}
}

Expand All @@ -401,13 +403,17 @@ public void handleSuccessfulWrite(MutationBatchResult mutationBatchResult) {
@Override
public void handleRejectedWrite(int batchId, Status status) {
assertCallback("handleRejectedWrite");
ImmutableSortedMap<DocumentKey, MaybeDocument> changes = localStore.rejectBatch(batchId);

if (!changes.isEmpty()) {
logErrorIfInteresting(status, "Write failed at %s", changes.getMinKey().getPath());
}

// The local store may or may not be able to apply the write result and raise events immediately
// (depending on whether the watcher is caught up), so we raise user callbacks first so that
// they consistently happen before listen events.
notifyUser(batchId, status);

ImmutableSortedMap<DocumentKey, MaybeDocument> changes = localStore.rejectBatch(batchId);
emitNewSnapshot(changes, /*remoteEvent=*/ null);
}

Expand Down Expand Up @@ -551,4 +557,28 @@ public void handleCredentialChange(User user) {
// Notify remote store so it can restart its streams.
remoteStore.handleCredentialChange();
}

/**
* Logs the error as a warnings if it likely represents a developer mistake such as forgetting to
* create an index or permission denied.
*/
private void logErrorIfInteresting(Status error, String contextString, Object... contextArgs) {
if (errorIsInteresting(error)) {
String context = String.format(contextString, contextArgs);
Logger.warn("Firestore", "%s: %s", context, error);
}
}

private boolean errorIsInteresting(Status error) {
Status.Code code = error.getCode();
String description = error.getDescription() != null ? error.getDescription() : "";

if (code == Status.Code.FAILED_PRECONDITION && description.contains("requires an index")) {
return true;
} else if (code == Status.Code.PERMISSION_DENIED) {
return true;
}

return false;
}
}