Refactor away from expected error. Handle 404 as any other error. (#100383) (#100398)

Co-authored-by: Pete Hampton <pjhampton@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2021-05-20 10:59:53 -04:00 committed by GitHub
parent f6b686490d
commit ac09981bb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 50 deletions

View file

@ -115,23 +115,7 @@ const getMockedEsClient = (esClientMock: jest.Mock) => {
};
describe('error handling', () => {
it('handles a 404 when searching for space usage', async () => {
const { features, licensing, usageCollection, usageStatsService } = setup({
license: { isAvailable: true, type: 'basic' },
});
const collector = getSpacesUsageCollector(usageCollection as any, {
kibanaIndexConfig$: Rx.of({ kibana: { index: '.kibana' } }),
features,
licensing,
usageStatsServicePromise: Promise.resolve(usageStatsService),
});
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
esClient.search.mockRejectedValue({ status: 404 });
await collector.fetch(getMockFetchContext(esClient));
});
it('throws error for a non-404', async () => {
it('throws error if cluster unavailable', async () => {
const { features, licensing, usageCollection, usageStatsService } = setup({
license: { isAvailable: true, type: 'basic' },
});
@ -143,7 +127,7 @@ describe('error handling', () => {
});
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
const statusCodes = [401, 402, 403, 500];
const statusCodes = [401, 402, 403, 404, 500];
for (const statusCode of statusCodes) {
const error = { status: statusCode };
esClient.search.mockRejectedValue(error);

View file

@ -47,42 +47,31 @@ async function getSpacesUsage(
}
const knownFeatureIds = features.getKibanaFeatures().map((feature) => feature.id);
let resp: SpacesAggregationResponse | undefined;
try {
// @ts-expect-error `SearchResponse['hits']['total']` incorrectly expects `number` type instead of `{ value: number }`.
({ body: resp } = await esClient.search({
index: kibanaIndex,
body: {
track_total_hits: true,
query: {
term: {
type: {
value: 'space',
},
const { body: resp } = (await esClient.search({
index: kibanaIndex,
body: {
track_total_hits: true,
query: {
term: {
type: {
value: 'space',
},
},
aggs: {
disabledFeatures: {
terms: {
field: 'space.disabledFeatures',
include: knownFeatureIds,
size: knownFeatureIds.length,
},
},
},
size: 0,
},
}));
} catch (err) {
if (err.status === 404) {
return null;
}
aggs: {
disabledFeatures: {
terms: {
field: 'space.disabledFeatures',
include: knownFeatureIds,
size: knownFeatureIds.length,
},
},
},
size: 0,
},
})) as { body: SpacesAggregationResponse };
throw err;
}
const { hits, aggregations } = resp!;
const { hits, aggregations } = resp;
const count = hits?.total?.value ?? 0;
const disabledFeatureBuckets = aggregations?.disabledFeatures?.buckets ?? [];