[8.8] [Enterprise Search] [Search Application] Fix error message when creating duplicate search application (#158333) (#158458)

# Backport

This will backport the following commits from `main` to `8.8`:
- [[Enterprise Search] [Search Application] Fix error message when
creating duplicate search application
(#158333)](https://github.com/elastic/kibana/pull/158333)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Saarika
Bhasi","email":"55930906+saarikabhasi@users.noreply.github.com"},"sourceCommit":{"committedDate":"2023-05-25T12:57:45Z","message":"[Enterprise
Search] [Search Application] Fix error message when creating duplicate
search application (#158333)\n\n## Summar\r\n\r\n* Instead of showing
Status Code **502: Internal Server Error**, Shows\r\n**409 :
Conflict**\r\n* Shows `\"Search application name already taken. Choose
another name\"`\r\nwhen duplicate search application is
created.\r\n\r\n## Screen
Recording\r\n\r\n\r\n96615f59-6fa2-4e4c-9758-f128ec56e455\r\n\r\n<img
width=\"1728\"
alt=\"Response\"\r\nsrc=\"56368b62-1799-4749-a019-67154dfb1cca\">","sha":"0b93a956db6b1ac9e2db5a57e77c690372f76bb7","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v8.9.0","v8.8.1"],"number":158333,"url":"https://github.com/elastic/kibana/pull/158333","mergeCommit":{"message":"[Enterprise
Search] [Search Application] Fix error message when creating duplicate
search application (#158333)\n\n## Summar\r\n\r\n* Instead of showing
Status Code **502: Internal Server Error**, Shows\r\n**409 :
Conflict**\r\n* Shows `\"Search application name already taken. Choose
another name\"`\r\nwhen duplicate search application is
created.\r\n\r\n## Screen
Recording\r\n\r\n\r\n96615f59-6fa2-4e4c-9758-f128ec56e455\r\n\r\n<img
width=\"1728\"
alt=\"Response\"\r\nsrc=\"56368b62-1799-4749-a019-67154dfb1cca\">","sha":"0b93a956db6b1ac9e2db5a57e77c690372f76bb7"}},"sourceBranch":"main","suggestedTargetBranches":["8.8"],"targetPullRequestStates":[{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/158333","number":158333,"mergeCommit":{"message":"[Enterprise
Search] [Search Application] Fix error message when creating duplicate
search application (#158333)\n\n## Summar\r\n\r\n* Instead of showing
Status Code **502: Internal Server Error**, Shows\r\n**409 :
Conflict**\r\n* Shows `\"Search application name already taken. Choose
another name\"`\r\nwhen duplicate search application is
created.\r\n\r\n## Screen
Recording\r\n\r\n\r\n96615f59-6fa2-4e4c-9758-f128ec56e455\r\n\r\n<img
width=\"1728\"
alt=\"Response\"\r\nsrc=\"56368b62-1799-4749-a019-67154dfb1cca\">","sha":"0b93a956db6b1ac9e2db5a57e77c690372f76bb7"}},{"branch":"8.8","label":"v8.8.1","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Saarika Bhasi <55930906+saarikabhasi@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2023-05-25 10:11:50 -04:00 committed by GitHub
parent 3b54b080e3
commit de87c1417a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 10 deletions

View file

@ -20,6 +20,7 @@ export enum ErrorCode {
PIPELINE_IS_IN_USE = 'pipeline_is_in_use',
PIPELINE_NOT_FOUND = 'pipeline_not_found',
RESOURCE_NOT_FOUND = 'resource_not_found',
SEARCH_APPLICATION_ALREADY_EXISTS = 'search_application_already_exists',
UNAUTHORIZED = 'unauthorized',
UNCAUGHT_EXCEPTION = 'uncaught_exception',
}

View file

@ -7,7 +7,7 @@
import { LogicMounter } from '../../../__mocks__/kea_logic';
import { Status } from '../../../../../common/types/api';
import { HttpError, Status } from '../../../../../common/types/api';
import { KibanaLogic } from '../../../shared/kibana';
import { CreateEngineApiLogic } from '../../api/engines/create_engine_api_logic';
@ -76,6 +76,19 @@ describe('CreateEngineLogic', () => {
indices: ['search-index-01'],
});
});
it('createEngine returns error when duplicate search application is created', () => {
const httpError: HttpError = {
body: {
error: 'search_application_already_exists',
message: 'Search application name already taken. Choose another name.',
statusCode: 409,
},
fetchOptions: {},
request: {},
} as HttpError;
CreateEngineApiLogic.actions.apiError(httpError);
expect(CreateEngineLogic.values.createEngineError).toEqual(httpError);
});
it('engineCreated is handled and is navigated to Search application list page', () => {
jest.spyOn(CreateEngineLogic.actions, 'fetchEngines');

View file

@ -266,6 +266,32 @@ describe('engines routes', () => {
mockRouter.shouldThrow(request);
});
it('returns 409 when upsert create search application throws error', async () => {
(mockClient.asCurrentUser.transport.request as jest.Mock).mockRejectedValueOnce({
meta: {
body: {
error: {
type: 'version_conflict_engine_exception',
},
},
statusCode: 409,
},
name: 'elasticsearch-js',
});
await mockRouter.callRoute({
params: { engine_name: 'engine-name' },
});
expect(mockRouter.response.customError).toHaveBeenCalledWith({
body: {
attributes: {
error_code: 'search_application_already_exists',
},
message: 'Search application name already taken. Choose another name.',
},
statusCode: 409,
});
});
});
describe('DELETE /internal/enterprise_search/engines/{engine_name}', () => {

View file

@ -6,6 +6,7 @@
*/
import { SearchResponse, AcknowledgedResponseBase } from '@elastic/elasticsearch/lib/api/types';
import { schema } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';
import {
EnterpriseSearchEngine,
@ -21,7 +22,10 @@ import { RouteDependencies } from '../../plugin';
import { createError } from '../../utils/create_error';
import { elasticsearchErrorHandler } from '../../utils/elasticsearch_error_handler';
import { isNotFoundException } from '../../utils/identify_exceptions';
import {
isNotFoundException,
isVersionConflictEngineException,
} from '../../utils/identify_exceptions';
export function registerEnginesRoutes({ log, router }: RouteDependencies) {
router.get(
@ -88,14 +92,32 @@ export function registerEnginesRoutes({ log, router }: RouteDependencies) {
},
elasticsearchErrorHandler(log, async (context, request, response) => {
const { client } = (await context.core).elasticsearch;
const engine =
await client.asCurrentUser.transport.request<EnterpriseSearchEngineUpsertResponse>({
body: { indices: request.body.indices },
method: 'PUT',
path: `/_application/search_application/${request.params.engine_name}`,
querystring: request.query,
});
return response.ok({ body: engine });
try {
const engine =
await client.asCurrentUser.transport.request<EnterpriseSearchEngineUpsertResponse>({
body: { indices: request.body.indices },
method: 'PUT',
path: `/_application/search_application/${request.params.engine_name}`,
querystring: request.query,
});
return response.ok({ body: engine });
} catch (error) {
if (isVersionConflictEngineException(error)) {
return createError({
errorCode: ErrorCode.SEARCH_APPLICATION_ALREADY_EXISTS,
message: i18n.translate(
'xpack.enterpriseSearch.server.routes.createSearchApplication.searchApplciationExistsError',
{
defaultMessage: 'Search application name already taken. Choose another name.',
}
),
response,
statusCode: 409,
});
}
throw error;
}
})
);

View file

@ -39,3 +39,6 @@ export const isNotFoundException = (error: ElasticsearchResponseError) =>
export const isIllegalArgumentException = (error: ElasticsearchResponseError) =>
error.meta?.body?.error?.type === 'illegal_argument_exception';
export const isVersionConflictEngineException = (error: ElasticsearchResponseError) =>
error.meta?.body?.error?.type === 'version_conflict_engine_exception';