Skip to content

Commit cf9893b

Browse files
committed
fix: enhance error handling for Firestore queries and improve test validations
- Added special handling for Firebase index errors in the query error response, providing a clear message and a link to create the required composite index. - Updated tests to filter and verify the presence of specific test documents instead of checking total count, ensuring more accurate validation of query results.
1 parent 92b0548 commit cf9893b

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

src/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,29 @@ class FirebaseMcpServer {
831831
};
832832
} catch (error) {
833833
logger.error('Error in collection group query:', error);
834+
835+
// Special handling for Firebase index errors
836+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
837+
if (errorMessage.includes('FAILED_PRECONDITION') && errorMessage.includes('requires an index')) {
838+
const indexUrl = errorMessage.match(/https:\/\/console\.firebase\.google\.com[^\s]*/)?.[0];
839+
return {
840+
content: [{
841+
type: 'text',
842+
text: JSON.stringify({
843+
error: 'This query requires a composite index.',
844+
details: 'When ordering by multiple fields or combining filters with ordering, you need to create a composite index.',
845+
indexUrl: indexUrl || null,
846+
message: errorMessage
847+
})
848+
}]
849+
};
850+
}
851+
834852
return {
835853
content: [{
836854
type: 'text',
837855
text: JSON.stringify({
838-
error: error instanceof Error ? error.message : 'Unknown error'
856+
error: errorMessage
839857
})
840858
}]
841859
};

src/lib/firebase/__tests__/firestoreClient.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -747,13 +747,21 @@ describe('Firestore Client', () => {
747747
// Parse the response
748748
const responseData = JSON.parse(result.content[0].text);
749749

750-
// Verify documents array exists and has correct length
750+
// Verify documents array exists
751751
expect(Array.isArray(responseData.documents)).toBe(true);
752-
expect(responseData.documents.length).toBe(2); // Should find both comments
752+
753+
// Instead of checking total count, filter to only include our test documents
754+
const testDocuments = responseData.documents.filter((doc: any) =>
755+
doc.path.includes(`${testCollection}/${parentDoc1Id}/${subcollectionName}`) ||
756+
doc.path.includes(`${testCollection}/${parentDoc2Id}/${subcollectionName}`)
757+
);
758+
759+
// Verify we have our 2 test documents
760+
expect(testDocuments.length).toBe(2);
753761

754762
// Verify both documents are returned with correct data
755-
const document1 = responseData.documents.find((doc: any) => doc.id === 'comment1');
756-
const document2 = responseData.documents.find((doc: any) => doc.id === 'comment2');
763+
const document1 = testDocuments.find((doc: any) => doc.id === 'comment1');
764+
const document2 = testDocuments.find((doc: any) => doc.id === 'comment2');
757765

758766
expect(document1).toBeDefined();
759767
expect(document1.data.author).toBe('User A');

0 commit comments

Comments
 (0)