From 76ac34a9b7c0c5f24c5225e2b34e729a56aa110c Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Fri, 25 Aug 2023 18:27:30 +0200 Subject: [PATCH] [DataViews] Improve error handling of resolving indices (#164400) 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. --- .../server/routes/resolve_index.ts | 19 +++++++++++---- test/api_integration/apis/data_views/index.ts | 1 + .../apis/data_views/resolve_index/index.ts | 15 ++++++++++++ .../data_views/resolve_index/resolve_index.ts | 23 +++++++++++++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 test/api_integration/apis/data_views/resolve_index/index.ts create mode 100644 test/api_integration/apis/data_views/resolve_index/resolve_index.ts diff --git a/src/plugins/data_view_management/server/routes/resolve_index.ts b/src/plugins/data_view_management/server/routes/resolve_index.ts index 820e6de1c9d7da..c9207d020a3551 100644 --- a/src/plugins/data_view_management/server/routes/resolve_index.ts +++ b/src/plugins/data_view_management/server/routes/resolve_index.ts @@ -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( @@ -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); + } + } } ); } diff --git a/test/api_integration/apis/data_views/index.ts b/test/api_integration/apis/data_views/index.ts index 328c7c1162d1b3..72fb1f780311b9 100644 --- a/test/api_integration/apis/data_views/index.ts +++ b/test/api_integration/apis/data_views/index.ts @@ -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')); }); } diff --git a/test/api_integration/apis/data_views/resolve_index/index.ts b/test/api_integration/apis/data_views/resolve_index/index.ts new file mode 100644 index 00000000000000..846c6a485070f3 --- /dev/null +++ b/test/api_integration/apis/data_views/resolve_index/index.ts @@ -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')); + }); +} diff --git a/test/api_integration/apis/data_views/resolve_index/resolve_index.ts b/test/api_integration/apis/data_views/resolve_index/resolve_index.ts new file mode 100644 index 00000000000000..31039ed3201115 --- /dev/null +++ b/test/api_integration/apis/data_views/resolve_index/resolve_index.ts @@ -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)); + }); +}