Skip to content

Commit

Permalink
fix(firestore): ensure all index URLs are captured and passed to the …
Browse files Browse the repository at this point in the history
…user (#10674)
  • Loading branch information
russellwheatley committed Apr 4, 2023
1 parent cf7dd46 commit 9800435
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/scripts/firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ service cloud.firestore {
allow read, write: if true;
}

match /{path=**}/collection-group/{subId} {
allow read, write: if true;
}

match /firestore-bundle-tests/{document=**} {
allow read, write: if true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public FlutterFirebaseFirestoreException(
break;
case "FAILED_PRECONDITION":
code = "failed-precondition";
if (foundMessage.contains("query requires an index")
|| foundMessage.contains("ensure it has been indexed")) {
if (foundMessage.contains("index")) {
message = foundMessage;
} else {
message = ERROR_FAILED_PRECONDITION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ void runQueryTests() {
expect(groupSnapshot.docs[0].data()['foo'], equals(2));
expect(groupSnapshot.docs[1].data()['foo'], equals(1));
});

testWidgets(
'should respond with a FirebaseException, the query requires an index',
(_) async {
try {
await FirebaseFirestore.instance
.collectionGroup('collection-group')
.where('number', isGreaterThan: 1, isLessThan: 3)
.where('foo', isEqualTo: 'bar')
.get();
} catch (error) {
expect(
(error as FirebaseException).code,
equals('failed-precondition'),
);
expect(
error.message,
'The query requires an index',
);
}
});
});

/**
Expand Down Expand Up @@ -186,6 +207,27 @@ void runQueryTests() {
}
fail('Should have thrown a [FirebaseException]');
});

testWidgets(
'should respond with a FirebaseException, the query requires an index',
(_) async {
try {
await FirebaseFirestore.instance
.collection('flutter-tests')
.where('number', isGreaterThan: 1, isLessThan: 3)
.where('foo', isEqualTo: 'bar')
.get();
} catch (error) {
expect(
(error as FirebaseException).code,
equals('failed-precondition'),
);
expect(
error.message,
'The query requires an index',
);
}
});
});

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ + (NSArray *)ErrorCodeAndMessageFromNSError:(NSError *)error {
break;
case FIRFirestoreErrorCodeFailedPrecondition:
code = @"failed-precondition";
if ([error.localizedDescription containsString:@"query requires an index"] ||
[error.localizedDescription containsString:@"requires a COLLECTION_GROUP_DESC index"] ||
[error.localizedDescription containsString:@"requires a COLLECTION_GROUP_ASC index"]) {
if ([error.localizedDescription containsString:@"index"]) {
message = error.localizedDescription;
} else {
message = @"Operation was rejected because the system is not in a state required for the "
Expand Down

0 comments on commit 9800435

Please sign in to comment.