Skip to content

Commit

Permalink
[DataViews] Improve error handling of resolving indices (elastic#164400)
Browse files Browse the repository at this point in the history
When in data view management an exact match index pattern is entered (without wildcard), there's now a status code of 404 instead of 500 returned, and no more error message logged.
  • Loading branch information
kertal authored Aug 25, 2023
1 parent 37195cc commit 76ac34a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/plugins/data_view_management/server/routes/resolve_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { schema } from '@kbn/config-schema';
import { IRouter } from '@kbn/core/server';
import { getKbnServerError } from '@kbn/kibana-utils-plugin/server';

export function registerResolveIndexRoute(router: IRouter): void {
router.get(
Expand All @@ -32,11 +33,19 @@ export function registerResolveIndexRoute(router: IRouter): void {
},
async (context, req, res) => {
const esClient = (await context.core).elasticsearch.client;
const body = await esClient.asCurrentUser.indices.resolveIndex({
name: req.params.query,
expand_wildcards: req.query.expand_wildcards || 'open',
});
return res.ok({ body });
try {
const body = await esClient.asCurrentUser.indices.resolveIndex({
name: req.params.query,
expand_wildcards: req.query.expand_wildcards || 'open',
});
return res.ok({ body });
} catch (e) {
if (e?.meta.statusCode === 404) {
return res.notFound({ body: { message: e.meta?.body?.error?.reason } });
} else {
throw getKbnServerError(e);
}
}
}
);
}
1 change: 1 addition & 0 deletions test/api_integration/apis/data_views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./deprecations'));
loadTestFile(require.resolve('./has_user_index_pattern'));
loadTestFile(require.resolve('./swap_references'));
loadTestFile(require.resolve('./resolve_index'));
});
}
15 changes: 15 additions & 0 deletions test/api_integration/apis/data_views/resolve_index/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { FtrProviderContext } from '../../../ftr_provider_context';

export default function ({ loadTestFile }: FtrProviderContext) {
describe('/internal/index-pattern-management/resolve_index', () => {
loadTestFile(require.resolve('./resolve_index'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { FtrProviderContext } from '../../../ftr_provider_context';

// node scripts/functional_tests --config test/api_integration/config.js --grep="Resolve index API"

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');

describe('Resolve index API', function () {
it('should return 200 for a search for indices with wildcard', () =>
supertest.get(`/internal/index-pattern-management/resolve_index/test*`).expect(200));

it('should return 404 for an exact match index', () =>
supertest.get(`/internal/index-pattern-management/resolve_index/test`).expect(404));
});
}

0 comments on commit 76ac34a

Please sign in to comment.