diff --git a/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.test.ts b/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.test.ts index 74db9a749b3e7..ffdd47e5cdf49 100644 --- a/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.test.ts +++ b/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.test.ts @@ -8,26 +8,25 @@ import { IndexPatternsFetcher } from '.'; import { ElasticsearchClient } from 'kibana/server'; +import * as indexNotFoundException from '../../../common/search/test_data/index_not_found_exception.json'; describe('Index Pattern Fetcher - server', () => { let indexPatterns: IndexPatternsFetcher; let esClient: ElasticsearchClient; const emptyResponse = { body: { - indices: [], + count: 0, }, }; const response = { body: { - indices: ['hello', 'world'], + count: 1115, }, }; const patternList = ['a', 'b', 'c']; beforeEach(() => { esClient = ({ - transport: { - request: jest.fn().mockReturnValueOnce(emptyResponse).mockReturnValue(response), - }, + count: jest.fn().mockResolvedValueOnce(emptyResponse).mockResolvedValue(response), } as unknown) as ElasticsearchClient; indexPatterns = new IndexPatternsFetcher(esClient); }); @@ -39,12 +38,35 @@ describe('Index Pattern Fetcher - server', () => { it('Returns all patterns when all match indices', async () => { esClient = ({ - transport: { - request: jest.fn().mockReturnValue(response), - }, + count: jest.fn().mockResolvedValue(response), } as unknown) as ElasticsearchClient; indexPatterns = new IndexPatternsFetcher(esClient); const result = await indexPatterns.validatePatternListActive(patternList); expect(result).toEqual(patternList); }); + it('Removes pattern when "index_not_found_exception" error is thrown', async () => { + class ServerError extends Error { + public body?: Record; + constructor( + message: string, + public readonly statusCode: number, + errBody?: Record + ) { + super(message); + this.body = errBody; + } + } + + esClient = ({ + count: jest + .fn() + .mockResolvedValueOnce(response) + .mockRejectedValue( + new ServerError('index_not_found_exception', 404, indexNotFoundException) + ), + } as unknown) as ElasticsearchClient; + indexPatterns = new IndexPatternsFetcher(esClient); + const result = await indexPatterns.validatePatternListActive(patternList); + expect(result).toEqual([patternList[0]]); + }); }); diff --git a/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.ts b/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.ts index 60c0c0b8c0607..3acdde33f599e 100644 --- a/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.ts +++ b/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.ts @@ -135,11 +135,20 @@ export class IndexPatternsFetcher { */ async validatePatternListActive(patternList: string[]) { const result = await Promise.all( - patternList.map((pattern) => - this.elasticsearchClient.count({ - index: pattern, - }) - ) + patternList + .map((pattern) => + this.elasticsearchClient.count({ + index: pattern, + }) + ) + .map((p) => + p.catch((e) => { + if (e.body.error.type === 'index_not_found_exception') { + return { body: { count: 0 } }; + } + throw e; + }) + ) ); return result.reduce( (acc: string[], { body: { count } }, patternListIndex) =>