Skip to content

Commit

Permalink
Add catch for error thrown on non-wildcard non-existing patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic committed Feb 5, 2021
1 parent 9b717c9 commit df0f7c9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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<string, any>;
constructor(
message: string,
public readonly statusCode: number,
errBody?: Record<string, any>
) {
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]]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down

0 comments on commit df0f7c9

Please sign in to comment.