Skip to content

Commit

Permalink
[Workplace Search] Fix bug where error was behind modal stuck in load…
Browse files Browse the repository at this point in the history
…ing state (elastic#104360)

* Fix an issue from previous PR

In elastic#104024, the error handling incorrectly used the `message` property on the response, when it should have been the attributes.errors array.

* Use inline error for duplicate name
  • Loading branch information
scottybollinger authored and darnautov committed Jul 7, 2021
1 parent 8766cbf commit 0c2198d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.']);
});
});

Expand Down Expand Up @@ -393,8 +394,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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,15 @@ export const SchemaLogic = kea<MakeLogicType<SchemaValues, SchemaActions>>({
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;
Expand Down Expand Up @@ -350,8 +350,8 @@ export const SchemaLogic = kea<MakeLogicType<SchemaValues, SchemaActions>>({
} 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);
Expand Down

0 comments on commit 0c2198d

Please sign in to comment.