From 3295dc4e55c20bb120f8ad278b8420bcbad84a14 Mon Sep 17 00:00:00 2001 From: scottybollinger Date: Mon, 5 Jul 2021 16:59:34 -0500 Subject: [PATCH 1/2] Fix an issue from previous PR In https://github.com/elastic/kibana/pull/104024, the error handling incorrectly used the `message` property on the response, when it should have been the attributes.errors array. --- .../content_sources/components/schema/schema_logic.test.ts | 6 ++++-- .../views/content_sources/components/schema/schema_logic.ts | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.test.ts index 3e8322145dad6..1d1b47d154eaa 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.test.ts @@ -393,8 +393,10 @@ describe('SchemaLogic', () => { it('handles error with message', async () => { const onSchemaSetFormErrorsSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetFormErrors'); - // We expect body.message to be a string[] when it is present - http.post.mockReturnValue(Promise.reject({ body: { message: ['this is an error'] } })); + // We expect body.attributes.errors to be a string[] when it is present + http.post.mockReturnValue( + Promise.reject({ body: { attributes: { errors: ['this is an error'] } } }) + ); SchemaLogic.actions.setServerField(schema, ADD); await nextTick(); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.ts index 7af074d412a60..92420cef919cb 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.ts @@ -350,8 +350,8 @@ export const SchemaLogic = kea>({ } catch (e) { window.scrollTo(0, 0); if (isAdding) { - // We expect body.message to be a string[] for actions.onSchemaSetFormErrors - const message: string[] = e?.body?.message || [defaultErrorMessage]; + // We expect body.attributes.errors to be a string[] for actions.onSchemaSetFormErrors + const message: string[] = e?.body?.attributes?.errors || [defaultErrorMessage]; actions.onSchemaSetFormErrors(message); } else { flashAPIErrors(e); From 4e7d3ba7e8d7f427b565fc5e1602617befd47dc1 Mon Sep 17 00:00:00 2001 From: scottybollinger Date: Mon, 5 Jul 2021 17:04:08 -0500 Subject: [PATCH 2/2] Use inline error for duplicate name --- .../content_sources/components/schema/schema_logic.test.ts | 3 ++- .../views/content_sources/components/schema/schema_logic.ts | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.test.ts index 1d1b47d154eaa..d642900aea169 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.test.ts @@ -325,10 +325,11 @@ describe('SchemaLogic', () => { }); it('handles duplicate', () => { + const onSchemaSetFormErrorsSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetFormErrors'); SchemaLogic.actions.onInitializeSchema(serverResponse); SchemaLogic.actions.addNewField('foo', SchemaType.Number); - expect(setErrorMessage).toHaveBeenCalledWith('New field already exists: foo.'); + expect(onSchemaSetFormErrorsSpy).toHaveBeenCalledWith(['New field already exists: foo.']); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.ts index 92420cef919cb..f43be974102b2 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/schema/schema_logic.ts @@ -301,15 +301,15 @@ export const SchemaLogic = kea>({ addNewField: ({ fieldName, newFieldType }) => { if (fieldName in values.activeSchema) { window.scrollTo(0, 0); - setErrorMessage( + actions.onSchemaSetFormErrors([ i18n.translate( 'xpack.enterpriseSearch.workplaceSearch.contentSource.schema.newFieldExists.message', { defaultMessage: 'New field already exists: {fieldName}.', values: { fieldName }, } - ) - ); + ), + ]); } else { const schema = cloneDeep(values.activeSchema); schema[fieldName] = newFieldType;