Skip to content

Commit

Permalink
[AO] Handle buildEsQuery error (such as leading wildcard) in status c…
Browse files Browse the repository at this point in the history
…hange (elastic#159891)

Fixes elastic#159079

## Summary

In the case of providing a wildcard in the search query, an error might
be generated depending on whether the related setting is enabled or not.
This PR tries to handle this error on the Alerts page for a better user
experience.

|Before|After|
|---|---|

|![image](https://github.com/elastic/kibana/assets/12370520/f38e4bc7-f900-4c73-8e6e-c1989eda57ad)|![image](https://github.com/elastic/kibana/assets/12370520/cf74577e-10ab-4543-8135-f498dcc7cabf)|

(cherry picked from commit 229e8ca)
  • Loading branch information
maryam-saeidi committed Jun 19, 2023
1 parent 8109a55 commit 06cc693
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';
import { waitFor } from '@testing-library/react';
import { timefilterServiceMock } from '@kbn/data-plugin/public/query/timefilter/timefilter_service.mock';
import { ObservabilityAlertSearchBarProps } from './types';
import { ObservabilityAlertSearchBarProps, Services } from './types';
import { ObservabilityAlertSearchBar } from './alert_search_bar';
import { observabilityAlertFeatureIds } from '../../../config/alert_feature_ids';
import { render } from '../../../utils/test_helper';
Expand All @@ -17,7 +17,10 @@ const getAlertsSearchBarMock = jest.fn();
const ALERT_SEARCH_BAR_DATA_TEST_SUBJ = 'alerts-search-bar';

describe('ObservabilityAlertSearchBar', () => {
const renderComponent = (props: Partial<ObservabilityAlertSearchBarProps> = {}) => {
const renderComponent = (
props: Partial<ObservabilityAlertSearchBarProps> = {},
services: Partial<Services> = {}
) => {
const observabilityAlertSearchBarProps: ObservabilityAlertSearchBarProps = {
appName: 'testAppName',
kuery: '',
Expand All @@ -35,6 +38,7 @@ describe('ObservabilityAlertSearchBar', () => {
<div data-test-subj={ALERT_SEARCH_BAR_DATA_TEST_SUBJ} />
),
useToasts: jest.fn(),
...services,
},
...props,
};
Expand Down Expand Up @@ -152,4 +156,26 @@ describe('ObservabilityAlertSearchBar', () => {
},
});
});

it('should show error in a toast', async () => {
const error = new Error('something is wrong in esQueryChange');
const mockedOnEsQueryChange = jest.fn().mockImplementation(() => {
throw error;
});
const mockedAddError = jest.fn();
const mockedUseToast = jest.fn().mockImplementation(() => ({
addError: mockedAddError,
}));

renderComponent(
{
onEsQueryChange: mockedOnEsQueryChange,
},
{
useToasts: mockedUseToast,
}
);

expect(mockedAddError).toHaveBeenCalledWith(error, { title: 'Invalid query string' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const getAlertStatusQuery = (status: string): Query[] => {
? [{ query: ALERT_STATUS_QUERY[status], language: 'kuery' }]
: [];
};
const toastTitle = i18n.translate('xpack.observability.alerts.searchBar.invalidQueryTitle', {
defaultMessage: 'Invalid query string',
});

export function ObservabilityAlertSearchBar({
appName,
Expand All @@ -41,18 +44,25 @@ export function ObservabilityAlertSearchBar({

const onAlertStatusChange = useCallback(
(alertStatus: AlertStatus) => {
onEsQueryChange(
buildEsQuery(
{
to: rangeTo,
from: rangeFrom,
},
kuery,
[...getAlertStatusQuery(alertStatus), ...defaultSearchQueries]
)
);
try {
onEsQueryChange(
buildEsQuery(
{
to: rangeTo,
from: rangeFrom,
},
kuery,
[...getAlertStatusQuery(alertStatus), ...defaultSearchQueries]
)
);
} catch (error) {
toasts.addError(error, {
title: toastTitle,
});
onKueryChange(DEFAULT_QUERY_STRING);
}
},
[kuery, defaultSearchQueries, rangeFrom, rangeTo, onEsQueryChange]
[onEsQueryChange, rangeTo, rangeFrom, kuery, defaultSearchQueries, toasts, onKueryChange]
);

useEffect(() => {
Expand Down Expand Up @@ -83,9 +93,7 @@ export function ObservabilityAlertSearchBar({
onEsQueryChange(esQuery);
} catch (error) {
toasts.addError(error, {
title: i18n.translate('xpack.observability.alerts.searchBar.invalidQueryTitle', {
defaultMessage: 'Invalid query string',
}),
title: toastTitle,
});
onKueryChange(DEFAULT_QUERY_STRING);
}
Expand Down

0 comments on commit 06cc693

Please sign in to comment.