Skip to content

Firestore: Fix "missing index" error message #6712

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 15 commits into from
Oct 27, 2022
Merged
16 changes: 15 additions & 1 deletion packages/firestore/src/platform/browser_lite/fetch_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,23 @@ export class FetchConnection extends RestConnection {
}

if (!response.ok) {
let errorMessage;
try {
// Try to get any error message text from server response.
//response.json returns an error object inside an array.
const [errorResponse] = (await response.json()) as [
{
error?: { message?: string };
}
];
if (errorResponse.error?.message) {
errorMessage = errorResponse.error.message;
}
} catch (_ignored) {}
throw new FirestoreError(
mapCodeFromHttpStatus(response.status),
'Request failed with error: ' + response.statusText
//response body is an array with error object
'Request failed with error: ' + errorMessage ?? response.statusText
);
}

Expand Down
21 changes: 21 additions & 0 deletions packages/firestore/test/integration/api/aggregation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
withTestCollection,
withTestDb
} from '../util/helpers';
import { USE_EMULATOR } from '../util/settings';

apiDescribe('Count quries', (persistence: boolean) => {
it('can run count query getCountFromServer', () => {
Expand Down Expand Up @@ -118,4 +119,24 @@ apiDescribe('Count quries', (persistence: boolean) => {
);
});
});

// Only verify the error message for missing indexes when running against
// production, since the Firestore Emulator does not require index creation
// and will, therefore, never fail in this situation.
// eslint-disable-next-line no-restricted-properties
(USE_EMULATOR ? it.skip : it)(
'getCountFromServer error message is good if missing index',
() => {
return withEmptyTestCollection(persistence, async coll => {
const query_ = query(
coll,
where('key1', '==', 42),
where('key2', '<', 42)
);
await expect(getCountFromServer(query_)).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
});
}
);
});
23 changes: 22 additions & 1 deletion packages/firestore/test/lite/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ import { runTransaction } from '../../src/lite-api/transaction';
import { writeBatch } from '../../src/lite-api/write_batch';
import {
DEFAULT_PROJECT_ID,
DEFAULT_SETTINGS
DEFAULT_SETTINGS,
USE_EMULATOR
} from '../integration/util/settings';

import {
Expand Down Expand Up @@ -2380,4 +2381,24 @@ describe('Count quries', () => {
expect(snapshot.data().count).to.equal(3);
});
});

// Only verify the error message for missing indexes when running against
// production, since the Firestore Emulator does not require index creation
// and will, therefore, never fail in this situation.
// eslint-disable-next-line no-restricted-properties
(USE_EMULATOR ? it.skip : it)(
'getCount error message is good if missing index',
() => {
return withTestCollection(async coll => {
const query_ = query(
coll,
where('key1', '==', 42),
where('key2', '<', 42)
);
await expect(getCount(query_)).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
});
}
);
});