From 81e70e33169b1b5557a6de906558b44a304095bf Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Wed, 15 Jul 2020 23:13:58 -0500 Subject: [PATCH 01/11] Add new hook to wrap the toasts service When receiving error responses from our APIs, this gives us better toast messages. --- .../common/hooks/use_app_toasts.test.ts | 60 +++++++++++++++++++ .../public/common/hooks/use_app_toasts.ts | 42 +++++++++++++ .../common/lib/kibana/__mocks__/index.ts | 2 + 3 files changed, 104 insertions(+) create mode 100644 x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts create mode 100644 x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts diff --git a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts new file mode 100644 index 0000000000000..e86ffc713bf70 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts @@ -0,0 +1,60 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { renderHook } from '@testing-library/react-hooks'; + +import { useToasts } from '../lib/kibana'; +import { useAppToasts } from './use_app_toasts'; + +jest.mock('../lib/kibana'); + +describe('useDeleteList', () => { + let addErrorMock: jest.Mock; + + beforeEach(() => { + addErrorMock = jest.fn(); + (useToasts as jest.Mock).mockImplementation(() => ({ + addError: addErrorMock, + })); + }); + + it('works normally with a regular error', async () => { + const error = new Error('regular error'); + const { result } = renderHook(() => useAppToasts()); + + result.current.addError(error, { title: 'title' }); + + expect(addErrorMock).toHaveBeenCalledWith(error, { title: 'title' }); + }); + + it("uses a KibanaApiError's body.message as the toastMessage", async () => { + const kibanaApiError = { + message: 'Not Found', + body: { status_code: 404, message: 'Detailed Message' }, + }; + + const { result } = renderHook(() => useAppToasts()); + + result.current.addError(kibanaApiError, { title: 'title' }); + + expect(addErrorMock).toHaveBeenCalledWith(kibanaApiError, { + title: 'title', + toastMessage: 'Detailed Message', + }); + }); + + it('converts an unknown error to an Error', () => { + const unknownError = undefined; + + const { result } = renderHook(() => useAppToasts()); + + result.current.addError(unknownError, { title: 'title' }); + + expect(addErrorMock).toHaveBeenCalledWith(Error(`${undefined}`), { + title: 'title', + }); + }); +}); diff --git a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts new file mode 100644 index 0000000000000..9d8f47eb5a4fd --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts @@ -0,0 +1,42 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { useCallback } from 'react'; + +import { ErrorToastOptions } from '../../../../../../src/core/public'; +import { useToasts } from '../lib/kibana'; +import { KibanaApiError, isApiError } from '../utils/api'; + +export const useAppToasts = () => { + const toasts = useToasts(); + + const addApiError = useCallback( + (error: KibanaApiError, options: ErrorToastOptions) => { + toasts.addError(error, { + ...options, + toastMessage: error.body.message, + }); + }, + [toasts] + ); + + const addError = useCallback( + (error: unknown, options: ErrorToastOptions) => { + if (isApiError(error)) { + addApiError(error, options); + } else { + if (error instanceof Error) { + toasts.addError(error, options); + } else { + toasts.addError(new Error(String(error)), options); + } + } + }, + [addApiError, toasts] + ); + + return { ...toasts, addError }; +}; diff --git a/x-pack/plugins/security_solution/public/common/lib/kibana/__mocks__/index.ts b/x-pack/plugins/security_solution/public/common/lib/kibana/__mocks__/index.ts index c3e1f35f37356..6ada887ece175 100644 --- a/x-pack/plugins/security_solution/public/common/lib/kibana/__mocks__/index.ts +++ b/x-pack/plugins/security_solution/public/common/lib/kibana/__mocks__/index.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { notificationServiceMock } from '../../../../../../../../src/core/public/mocks'; import { createKibanaContextProviderMock, createUseUiSettingMock, @@ -19,6 +20,7 @@ export const useUiSetting$ = jest.fn(createUseUiSetting$Mock()); export const useTimeZone = jest.fn(); export const useDateFormat = jest.fn(); export const useBasePath = jest.fn(() => '/test/base/path'); +export const useToasts = jest.fn(() => notificationServiceMock.createStartContract().toasts); export const useCurrentUser = jest.fn(); export const withKibana = jest.fn(createWithKibanaMock()); export const KibanaContextProvider = jest.fn(createKibanaContextProviderMock()); From 28095e6152e11e338243b36d4616fcec740c1f36 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Wed, 15 Jul 2020 23:33:12 -0500 Subject: [PATCH 02/11] Replace useToasts with useAppToasts in trivial case --- .../components/value_lists_management_modal/modal.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/components/value_lists_management_modal/modal.tsx b/x-pack/plugins/security_solution/public/detections/components/value_lists_management_modal/modal.tsx index 0a935a9cdb1c4..645c6750ba6ed 100644 --- a/x-pack/plugins/security_solution/public/detections/components/value_lists_management_modal/modal.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/value_lists_management_modal/modal.tsx @@ -23,7 +23,8 @@ import { useDeleteList, useCursor, } from '../../../shared_imports'; -import { useToasts, useKibana } from '../../../common/lib/kibana'; +import { useKibana } from '../../../common/lib/kibana'; +import { useAppToasts } from '../../../common/hooks/use_app_toasts'; import { GenericDownloader } from '../../../common/components/generic_downloader'; import * as i18n from './translations'; import { ValueListsTable } from './table'; @@ -45,7 +46,7 @@ export const ValueListsModalComponent: React.FC = ({ const { start: findLists, ...lists } = useFindLists(); const { start: deleteList, result: deleteResult } = useDeleteList(); const [exportListId, setExportListId] = useState(); - const toasts = useToasts(); + const toasts = useAppToasts(); const fetchLists = useCallback(() => { findLists({ cursor, http, pageIndex: pageIndex + 1, pageSize }); From 940c54bb8c93b830a8d4f4360ad4c7009e6f02c2 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Wed, 15 Jul 2020 23:33:59 -0500 Subject: [PATCH 03/11] WIP: prevent infinite polling when server is unresponsive The crux of this issue was that we had no steady state when the server returned a non-API error (!isApiError), as it would if the server was throwing 500s or just generally misbehaving. The solution, then, is to addresse these non-API errors in our underlying useListsIndex and useListsPrivileges hooks. This also refactors those hooks to: * collapse multiple error states into one (that's all we currently need) * use useAppToasts for better UI TODO: I don't think I need the changes in useListsConfig's useEffect. --- .../lists/use_lists_config.tsx | 10 +-- .../lists/use_lists_index.tsx | 63 ++++++++----------- .../lists/use_lists_privileges.tsx | 15 +++-- 3 files changed, 39 insertions(+), 49 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_config.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_config.tsx index e21cbceeaef27..ba2b550460fb1 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_config.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_config.tsx @@ -19,23 +19,23 @@ export interface UseListsConfigReturn { } export const useListsConfig = (): UseListsConfigReturn => { - const { createIndex, createIndexError, indexExists, loading: indexLoading } = useListsIndex(); + const { createIndex, indexExists, loading: indexLoading, error: indexError } = useListsIndex(); const { canManageIndex, canWriteIndex, loading: privilegesLoading } = useListsPrivileges(); const { lists } = useKibana().services; const enabled = lists != null; const loading = indexLoading || privilegesLoading; const needsIndex = indexExists === false; - const indexCreationFailed = createIndexError != null; + const hasIndexError = indexError != null; const needsIndexConfiguration = - needsIndex && (canManageIndex === false || (canManageIndex === true && indexCreationFailed)); + needsIndex && (canManageIndex === false || (canManageIndex === true && hasIndexError)); const needsConfiguration = !enabled || canWriteIndex === false || needsIndexConfiguration; useEffect(() => { - if (needsIndex && canManageIndex) { + if (needsIndex && canManageIndex && !hasIndexError) { createIndex(); } - }, [canManageIndex, createIndex, needsIndex]); + }, [canManageIndex, createIndex, hasIndexError, needsIndex]); return { canManageIndex, canWriteIndex, enabled, loading, needsConfiguration }; }; diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx index 75f12bd07d3ae..fa33e2cc17305 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx @@ -7,28 +7,24 @@ import { useEffect, useState, useCallback } from 'react'; import { useReadListIndex, useCreateListIndex } from '../../../../shared_imports'; -import { useHttp, useToasts, useKibana } from '../../../../common/lib/kibana'; +import { useHttp, useKibana } from '../../../../common/lib/kibana'; import { isApiError } from '../../../../common/utils/api'; import * as i18n from './translations'; +import { useAppToasts } from '../../../../common/hooks/use_app_toasts'; -export interface UseListsIndexState { +export interface UseListsIndexReturn { + createIndex: () => void; indexExists: boolean | null; -} - -export interface UseListsIndexReturn extends UseListsIndexState { + error: unknown; loading: boolean; - createIndex: () => void; - createIndexError: unknown; - createIndexResult: { acknowledged: boolean } | undefined; } export const useListsIndex = (): UseListsIndexReturn => { - const [state, setState] = useState({ - indexExists: null, - }); + const [indexExists, setIndexExists] = useState(null); + const [error, setError] = useState(null); const { lists } = useKibana().services; const http = useHttp(); - const toasts = useToasts(); + const toasts = useAppToasts(); const { loading: readLoading, start: readListIndex, ...readListIndexState } = useReadListIndex(); const { loading: createLoading, @@ -51,18 +47,17 @@ export const useListsIndex = (): UseListsIndexReturn => { // initial read list useEffect(() => { - if (!readLoading && state.indexExists === null) { + if (!readLoading && !error && indexExists === null) { readIndex(); } - }, [readIndex, readLoading, state.indexExists]); + }, [error, indexExists, readIndex, readLoading]); // handle read result useEffect(() => { if (readListIndexState.result != null) { - setState({ - indexExists: - readListIndexState.result.list_index && readListIndexState.result.list_item_index, - }); + setIndexExists( + readListIndexState.result.list_index && readListIndexState.result.list_item_index + ); } }, [readListIndexState.result]); @@ -75,34 +70,30 @@ export const useListsIndex = (): UseListsIndexReturn => { // handle read error useEffect(() => { - const error = readListIndexState.error; - if (isApiError(error)) { - setState({ indexExists: false }); - if (error.body.status_code !== 404) { - toasts.addError(error, { - title: i18n.LISTS_INDEX_FETCH_FAILURE, - toastMessage: error.body.message, - }); + const err = readListIndexState.error; + if (err != null) { + if (isApiError(err) && err.body.status_code === 404) { + setIndexExists(false); + } else { + setError(err); + toasts.addError(err, { title: i18n.LISTS_INDEX_FETCH_FAILURE }); } } }, [readListIndexState.error, toasts]); // handle create error useEffect(() => { - const error = createListIndexState.error; - if (isApiError(error)) { - toasts.addError(error, { - title: i18n.LISTS_INDEX_CREATE_FAILURE, - toastMessage: error.body.message, - }); + const err = createListIndexState.error; + if (err != null) { + setError(err); + toasts.addError(err, { title: i18n.LISTS_INDEX_CREATE_FAILURE }); } }, [createListIndexState.error, toasts]); return { - loading, createIndex, - createIndexError: createListIndexState.error, - createIndexResult: createListIndexState.result, - ...state, + error, + indexExists, + loading, }; }; diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx index fbbcff33402c3..77cb2590b9e6b 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx @@ -7,8 +7,8 @@ import { useEffect, useState, useCallback } from 'react'; import { useReadListPrivileges } from '../../../../shared_imports'; -import { useHttp, useToasts, useKibana } from '../../../../common/lib/kibana'; -import { isApiError } from '../../../../common/utils/api'; +import { useHttp, useKibana } from '../../../../common/lib/kibana'; +import { useAppToasts } from '../../../../common/hooks/use_app_toasts'; import * as i18n from './translations'; export interface UseListsPrivilegesState { @@ -79,7 +79,7 @@ export const useListsPrivileges = (): UseListsPrivilegesReturn => { }); const { lists } = useKibana().services; const http = useHttp(); - const toasts = useToasts(); + const toasts = useAppToasts(); const { loading, start: readListPrivileges, ...privilegesState } = useReadListPrivileges(); const readPrivileges = useCallback(() => { @@ -90,10 +90,10 @@ export const useListsPrivileges = (): UseListsPrivilegesReturn => { // initRead useEffect(() => { - if (!loading && state.isAuthenticated === null) { + if (!loading && !privilegesState.error && state.isAuthenticated === null) { readPrivileges(); } - }, [loading, readPrivileges, state.isAuthenticated]); + }, [loading, privilegesState.error, readPrivileges, state.isAuthenticated]); // handleReadResult useEffect(() => { @@ -119,11 +119,10 @@ export const useListsPrivileges = (): UseListsPrivilegesReturn => { // handleReadError useEffect(() => { const error = privilegesState.error; - if (isApiError(error)) { - setState({ isAuthenticated: null, canManageIndex: false, canWriteIndex: false }); + if (error != null) { + setState({ isAuthenticated: false, canManageIndex: false, canWriteIndex: false }); toasts.addError(error, { title: i18n.LISTS_PRIVILEGES_READ_FAILURE, - toastMessage: error.body.message, }); } }, [privilegesState.error, toasts]); From beefe5b8f84ec9a4af1772fd46bdadd043c537cd Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Wed, 15 Jul 2020 23:42:03 -0500 Subject: [PATCH 04/11] Slightly more legible variables The only task in this hook is our readPrivileges task right now, so I'm shortening the variable until we have a need to disambiguate it further. --- .../lists/use_lists_privileges.tsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx index 77cb2590b9e6b..27b640044d6b5 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx @@ -80,7 +80,7 @@ export const useListsPrivileges = (): UseListsPrivilegesReturn => { const { lists } = useKibana().services; const http = useHttp(); const toasts = useAppToasts(); - const { loading, start: readListPrivileges, ...privilegesState } = useReadListPrivileges(); + const { loading, start: readListPrivileges, ...readState } = useReadListPrivileges(); const readPrivileges = useCallback(() => { if (lists) { @@ -90,20 +90,20 @@ export const useListsPrivileges = (): UseListsPrivilegesReturn => { // initRead useEffect(() => { - if (!loading && !privilegesState.error && state.isAuthenticated === null) { + if (!loading && !readState.error && state.isAuthenticated === null) { readPrivileges(); } - }, [loading, privilegesState.error, readPrivileges, state.isAuthenticated]); + }, [loading, readState.error, readPrivileges, state.isAuthenticated]); // handleReadResult useEffect(() => { - if (privilegesState.result != null) { + if (readState.result != null) { try { const { is_authenticated: isAuthenticated, lists: { index: listsPrivileges }, listItems: { index: listItemsPrivileges }, - } = privilegesState.result as ListPrivileges; + } = readState.result as ListPrivileges; setState({ isAuthenticated, @@ -114,18 +114,18 @@ export const useListsPrivileges = (): UseListsPrivilegesReturn => { setState({ isAuthenticated: null, canManageIndex: false, canWriteIndex: false }); } } - }, [privilegesState.result]); + }, [readState.result]); // handleReadError useEffect(() => { - const error = privilegesState.error; + const error = readState.error; if (error != null) { setState({ isAuthenticated: false, canManageIndex: false, canWriteIndex: false }); toasts.addError(error, { title: i18n.LISTS_PRIVILEGES_READ_FAILURE, }); } - }, [privilegesState.error, toasts]); + }, [readState.error, toasts]); return { loading, ...state }; }; From 24ae1f5a0ce1e0aa47d60194dc784b967f9b5e88 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 16 Jul 2020 14:44:49 -0500 Subject: [PATCH 05/11] Remove unnecessary conditions around creating our index If the index hook has an error needsIndex will not be true. --- .../containers/detection_engine/lists/use_lists_config.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_config.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_config.tsx index ba2b550460fb1..71847e7b7d8cb 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_config.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_config.tsx @@ -32,10 +32,10 @@ export const useListsConfig = (): UseListsConfigReturn => { const needsConfiguration = !enabled || canWriteIndex === false || needsIndexConfiguration; useEffect(() => { - if (needsIndex && canManageIndex && !hasIndexError) { + if (needsIndex && canManageIndex) { createIndex(); } - }, [canManageIndex, createIndex, hasIndexError, needsIndex]); + }, [canManageIndex, createIndex, needsIndex]); return { canManageIndex, canWriteIndex, enabled, loading, needsConfiguration }; }; From f78d9bb4f4baa73ba73b50b0c33c33daa1e8334e Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 16 Jul 2020 15:17:11 -0500 Subject: [PATCH 06/11] Better toast errors for Kibana API errors Our isApiError predicate does not work for errors coming back from Kibana platform itself, e.g. for a request payload error. I've added a separate predicate for that case, isKibanaError, and then a wrapping isAppError predicate since most of our use cases just care about error.body.message, which is common to both. --- .../common/components/toasters/utils.ts | 4 ++-- .../common/hooks/use_app_toasts.test.ts | 2 +- .../public/common/hooks/use_app_toasts.ts | 12 +++++----- .../public/common/utils/api/index.ts | 23 +++++++++++++++++-- .../alerts/use_signal_index.tsx | 6 ++--- .../lists/use_lists_index.tsx | 4 ++-- 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/x-pack/plugins/security_solution/public/common/components/toasters/utils.ts b/x-pack/plugins/security_solution/public/common/components/toasters/utils.ts index e7cc389d4c06b..47c5588a12830 100644 --- a/x-pack/plugins/security_solution/public/common/components/toasters/utils.ts +++ b/x-pack/plugins/security_solution/public/common/components/toasters/utils.ts @@ -9,7 +9,7 @@ import { isError } from 'lodash/fp'; import { AppToast, ActionToaster } from './'; import { isToasterError } from './errors'; -import { isApiError } from '../../utils/api'; +import { isAppError } from '../../utils/api'; /** * Displays an error toast for the provided title and message @@ -114,7 +114,7 @@ export const errorToToaster = ({ iconType, errors: error.messages, }; - } else if (isApiError(error)) { + } else if (isAppError(error)) { toast = { id, title, diff --git a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts index e86ffc713bf70..c8508ae23a6ea 100644 --- a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts +++ b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts @@ -30,7 +30,7 @@ describe('useDeleteList', () => { expect(addErrorMock).toHaveBeenCalledWith(error, { title: 'title' }); }); - it("uses a KibanaApiError's body.message as the toastMessage", async () => { + it("uses a AppError's body.message as the toastMessage", async () => { const kibanaApiError = { message: 'Not Found', body: { status_code: 404, message: 'Detailed Message' }, diff --git a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts index 9d8f47eb5a4fd..315d78677bef7 100644 --- a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts +++ b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts @@ -8,13 +8,13 @@ import { useCallback } from 'react'; import { ErrorToastOptions } from '../../../../../../src/core/public'; import { useToasts } from '../lib/kibana'; -import { KibanaApiError, isApiError } from '../utils/api'; +import { isAppError, AppError } from '../utils/api'; export const useAppToasts = () => { const toasts = useToasts(); - const addApiError = useCallback( - (error: KibanaApiError, options: ErrorToastOptions) => { + const addAppError = useCallback( + (error: AppError, options: ErrorToastOptions) => { toasts.addError(error, { ...options, toastMessage: error.body.message, @@ -25,8 +25,8 @@ export const useAppToasts = () => { const addError = useCallback( (error: unknown, options: ErrorToastOptions) => { - if (isApiError(error)) { - addApiError(error, options); + if (isAppError(error)) { + addAppError(error, options); } else { if (error instanceof Error) { toasts.addError(error, options); @@ -35,7 +35,7 @@ export const useAppToasts = () => { } } }, - [addApiError, toasts] + [addAppError, toasts] ); return { ...toasts, addError }; diff --git a/x-pack/plugins/security_solution/public/common/utils/api/index.ts b/x-pack/plugins/security_solution/public/common/utils/api/index.ts index ab442d0d09cf9..e8934259fe43e 100644 --- a/x-pack/plugins/security_solution/public/common/utils/api/index.ts +++ b/x-pack/plugins/security_solution/public/common/utils/api/index.ts @@ -6,14 +6,33 @@ import { has } from 'lodash/fp'; -export interface KibanaApiError { +export interface AppError { name: string; message: string; + body: { + message: string; + }; +} + +export interface KibanaError extends AppError { + body: { + message: string; + statusCode: number; + }; +} + +export interface SecurityAppError extends AppError { body: { message: string; status_code: number; }; } -export const isApiError = (error: unknown): error is KibanaApiError => +export const isKibanaError = (error: unknown): error is KibanaError => + has('message', error) && has('body.message', error) && has('body.statusCode', error); + +export const isSecurityAppError = (error: unknown): error is SecurityAppError => has('message', error) && has('body.message', error) && has('body.status_code', error); + +export const isAppError = (error: unknown): error is AppError => + isKibanaError(error) || isSecurityAppError(error); diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_signal_index.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_signal_index.tsx index 65a2721013b5e..14fd9ffa50843 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_signal_index.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_signal_index.tsx @@ -9,7 +9,7 @@ import { useEffect, useState } from 'react'; import { errorToToaster, useStateToaster } from '../../../../common/components/toasters'; import { createSignalIndex, getSignalIndex } from './api'; import * as i18n from './translations'; -import { isApiError } from '../../../../common/utils/api'; +import { isSecurityAppError } from '../../../../common/utils/api'; type Func = () => void; @@ -59,7 +59,7 @@ export const useSignalIndex = (): ReturnSignalIndex => { signalIndexName: null, createDeSignalIndex: createIndex, }); - if (isApiError(error) && error.body.status_code !== 404) { + if (isSecurityAppError(error) && error.body.status_code !== 404) { errorToToaster({ title: i18n.SIGNAL_GET_NAME_FAILURE, error, dispatchToaster }); } } @@ -81,7 +81,7 @@ export const useSignalIndex = (): ReturnSignalIndex => { } } catch (error) { if (isSubscribed) { - if (isApiError(error) && error.body.status_code === 409) { + if (isSecurityAppError(error) && error.body.status_code === 409) { fetchData(); } else { setSignalIndex({ diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx index fa33e2cc17305..7eb9f024422e2 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx @@ -8,7 +8,7 @@ import { useEffect, useState, useCallback } from 'react'; import { useReadListIndex, useCreateListIndex } from '../../../../shared_imports'; import { useHttp, useKibana } from '../../../../common/lib/kibana'; -import { isApiError } from '../../../../common/utils/api'; +import { isSecurityAppError } from '../../../../common/utils/api'; import * as i18n from './translations'; import { useAppToasts } from '../../../../common/hooks/use_app_toasts'; @@ -72,7 +72,7 @@ export const useListsIndex = (): UseListsIndexReturn => { useEffect(() => { const err = readListIndexState.error; if (err != null) { - if (isApiError(err) && err.body.status_code === 404) { + if (isSecurityAppError(err) && err.body.status_code === 404) { setIndexExists(false); } else { setError(err); From 3dd2fd4ad42db1f5d6e3ddfacc31420c9b3a09e1 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 16 Jul 2020 15:28:12 -0500 Subject: [PATCH 07/11] Use new toasts hook on our exceptions modals This fixes two issues: * toast appears above modal overlay * Error message from response is now presented in the toast --- .../exceptions/add_exception_modal/index.tsx | 12 ++++++------ .../exceptions/edit_exception_modal/index.tsx | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx b/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx index d5eeef0f1e768..840c3124fb31c 100644 --- a/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx +++ b/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx @@ -29,8 +29,8 @@ import { } from '../../../../../public/lists_plugin_deps'; import * as i18n from './translations'; import { TimelineNonEcsData, Ecs } from '../../../../graphql/types'; +import { useAppToasts } from '../../../hooks/use_app_toasts'; import { useKibana } from '../../../lib/kibana'; -import { errorToToaster, displaySuccessToast, useStateToaster } from '../../toasters'; import { ExceptionBuilder } from '../builder'; import { Loader } from '../../loader'; import { useAddOrUpdateException } from '../use_add_exception'; @@ -115,7 +115,7 @@ export const AddExceptionModal = memo(function AddExceptionModal({ Array >([]); const [fetchOrCreateListError, setFetchOrCreateListError] = useState(false); - const [, dispatchToaster] = useStateToaster(); + const toasts = useAppToasts(); const { loading: isSignalIndexLoading, signalIndexName } = useSignalIndex(); const [{ isLoading: indexPatternLoading, indexPatterns }] = useFetchIndexPatterns( @@ -124,15 +124,15 @@ export const AddExceptionModal = memo(function AddExceptionModal({ const onError = useCallback( (error: Error) => { - errorToToaster({ title: i18n.ADD_EXCEPTION_ERROR, error, dispatchToaster }); + toasts.addError(error, { title: i18n.ADD_EXCEPTION_ERROR }); onCancel(); }, - [dispatchToaster, onCancel] + [onCancel, toasts] ); const onSuccess = useCallback(() => { - displaySuccessToast(i18n.ADD_EXCEPTION_SUCCESS, dispatchToaster); + toasts.addSuccess(i18n.ADD_EXCEPTION_SUCCESS); onConfirm(shouldCloseAlert); - }, [dispatchToaster, onConfirm, shouldCloseAlert]); + }, [onConfirm, shouldCloseAlert, toasts]); const [{ isLoading: addExceptionIsLoading }, addOrUpdateExceptionItems] = useAddOrUpdateException( { diff --git a/x-pack/plugins/security_solution/public/common/components/exceptions/edit_exception_modal/index.tsx b/x-pack/plugins/security_solution/public/common/components/exceptions/edit_exception_modal/index.tsx index 73933d483e2cb..579eda673a92e 100644 --- a/x-pack/plugins/security_solution/public/common/components/exceptions/edit_exception_modal/index.tsx +++ b/x-pack/plugins/security_solution/public/common/components/exceptions/edit_exception_modal/index.tsx @@ -30,7 +30,7 @@ import { } from '../../../../../public/lists_plugin_deps'; import * as i18n from './translations'; import { useKibana } from '../../../lib/kibana'; -import { errorToToaster, displaySuccessToast, useStateToaster } from '../../toasters'; +import { useAppToasts } from '../../../hooks/use_app_toasts'; import { ExceptionBuilder } from '../builder'; import { useAddOrUpdateException } from '../use_add_exception'; import { AddExceptionComments } from '../add_exception_comments'; @@ -93,7 +93,7 @@ export const EditExceptionModal = memo(function EditExceptionModal({ const [exceptionItemsToAdd, setExceptionItemsToAdd] = useState< Array >([]); - const [, dispatchToaster] = useStateToaster(); + const toasts = useAppToasts(); const { loading: isSignalIndexLoading, signalIndexName } = useSignalIndex(); const [{ isLoading: indexPatternLoading, indexPatterns }] = useFetchIndexPatterns( @@ -102,15 +102,15 @@ export const EditExceptionModal = memo(function EditExceptionModal({ const onError = useCallback( (error) => { - errorToToaster({ title: i18n.EDIT_EXCEPTION_ERROR, error, dispatchToaster }); + toasts.addError(error, { title: i18n.EDIT_EXCEPTION_ERROR }); onCancel(); }, - [dispatchToaster, onCancel] + [onCancel, toasts] ); const onSuccess = useCallback(() => { - displaySuccessToast(i18n.EDIT_EXCEPTION_SUCCESS, dispatchToaster); + toasts.addSuccess(i18n.EDIT_EXCEPTION_SUCCESS); onConfirm(); - }, [dispatchToaster, onConfirm]); + }, [onConfirm, toasts]); const [{ isLoading: addExceptionIsLoading }, addOrUpdateExceptionItems] = useAddOrUpdateException( { From b7553615f2e6be2ecef04e5bd2e174a3a492e68c Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 16 Jul 2020 17:39:30 -0500 Subject: [PATCH 08/11] Fix bug with toasts dependencies Because of the way some of the exception modal's hooks are written, a change to one of its callbacks means that the request will be canceled. Because the toasts service exports instance methods, the context within the function (and thus the function itself) can change leading to a mutable ref. Because we don't want/need this behavior, we store our exported functions in refs to 'freeze' them for react. With our bound functions, we should now be able to declare e.g. `toast.addError` as a dependency, however react cannot determine that it is bound (and thus that toast.addError() is equivalent to addError()), and so we must destructure our functions in order to use them as dependencies. --- .../exceptions/add_exception_modal/index.tsx | 10 +++--- .../exceptions/edit_exception_modal/index.tsx | 10 +++--- .../common/hooks/use_app_toasts.test.ts | 3 ++ .../public/common/hooks/use_app_toasts.ts | 34 +++++++++++-------- .../value_lists_management_modal/modal.tsx | 10 +++--- .../lists/use_lists_index.tsx | 10 +++--- .../lists/use_lists_privileges.tsx | 6 ++-- 7 files changed, 46 insertions(+), 37 deletions(-) diff --git a/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx b/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx index 840c3124fb31c..79383676266f5 100644 --- a/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx +++ b/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx @@ -115,7 +115,7 @@ export const AddExceptionModal = memo(function AddExceptionModal({ Array >([]); const [fetchOrCreateListError, setFetchOrCreateListError] = useState(false); - const toasts = useAppToasts(); + const { addError, addSuccess } = useAppToasts(); const { loading: isSignalIndexLoading, signalIndexName } = useSignalIndex(); const [{ isLoading: indexPatternLoading, indexPatterns }] = useFetchIndexPatterns( @@ -124,15 +124,15 @@ export const AddExceptionModal = memo(function AddExceptionModal({ const onError = useCallback( (error: Error) => { - toasts.addError(error, { title: i18n.ADD_EXCEPTION_ERROR }); + addError(error, { title: i18n.ADD_EXCEPTION_ERROR }); onCancel(); }, - [onCancel, toasts] + [addError, onCancel] ); const onSuccess = useCallback(() => { - toasts.addSuccess(i18n.ADD_EXCEPTION_SUCCESS); + addSuccess(i18n.ADD_EXCEPTION_SUCCESS); onConfirm(shouldCloseAlert); - }, [onConfirm, shouldCloseAlert, toasts]); + }, [addSuccess, onConfirm, shouldCloseAlert]); const [{ isLoading: addExceptionIsLoading }, addOrUpdateExceptionItems] = useAddOrUpdateException( { diff --git a/x-pack/plugins/security_solution/public/common/components/exceptions/edit_exception_modal/index.tsx b/x-pack/plugins/security_solution/public/common/components/exceptions/edit_exception_modal/index.tsx index 579eda673a92e..dbc70dfe21dd0 100644 --- a/x-pack/plugins/security_solution/public/common/components/exceptions/edit_exception_modal/index.tsx +++ b/x-pack/plugins/security_solution/public/common/components/exceptions/edit_exception_modal/index.tsx @@ -93,7 +93,7 @@ export const EditExceptionModal = memo(function EditExceptionModal({ const [exceptionItemsToAdd, setExceptionItemsToAdd] = useState< Array >([]); - const toasts = useAppToasts(); + const { addError, addSuccess } = useAppToasts(); const { loading: isSignalIndexLoading, signalIndexName } = useSignalIndex(); const [{ isLoading: indexPatternLoading, indexPatterns }] = useFetchIndexPatterns( @@ -102,15 +102,15 @@ export const EditExceptionModal = memo(function EditExceptionModal({ const onError = useCallback( (error) => { - toasts.addError(error, { title: i18n.EDIT_EXCEPTION_ERROR }); + addError(error, { title: i18n.EDIT_EXCEPTION_ERROR }); onCancel(); }, - [onCancel, toasts] + [addError, onCancel] ); const onSuccess = useCallback(() => { - toasts.addSuccess(i18n.EDIT_EXCEPTION_SUCCESS); + addSuccess(i18n.EDIT_EXCEPTION_SUCCESS); onConfirm(); - }, [onConfirm, toasts]); + }, [addSuccess, onConfirm]); const [{ isLoading: addExceptionIsLoading }, addOrUpdateExceptionItems] = useAddOrUpdateException( { diff --git a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts index c8508ae23a6ea..e0e629793952a 100644 --- a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts +++ b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts @@ -13,11 +13,14 @@ jest.mock('../lib/kibana'); describe('useDeleteList', () => { let addErrorMock: jest.Mock; + let addSuccessMock: jest.Mock; beforeEach(() => { addErrorMock = jest.fn(); + addSuccessMock = jest.fn(); (useToasts as jest.Mock).mockImplementation(() => ({ addError: addErrorMock, + addSuccess: addSuccessMock, })); }); diff --git a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts index 315d78677bef7..bc59d87100058 100644 --- a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts +++ b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts @@ -4,39 +4,45 @@ * you may not use this file except in compliance with the Elastic License. */ -import { useCallback } from 'react'; +import { useCallback, useRef } from 'react'; -import { ErrorToastOptions } from '../../../../../../src/core/public'; +import { ErrorToastOptions, ToastsStart, Toast } from '../../../../../../src/core/public'; import { useToasts } from '../lib/kibana'; import { isAppError, AppError } from '../utils/api'; -export const useAppToasts = () => { +export type UseAppToasts = Pick & { + api: ToastsStart; + addError: (error: unknown, options: ErrorToastOptions) => Toast; +}; + +export const useAppToasts = (): UseAppToasts => { const toasts = useToasts(); + const addError = useRef(toasts.addError.bind(toasts)).current; + const addSuccess = useRef(toasts.addSuccess.bind(toasts)).current; const addAppError = useCallback( - (error: AppError, options: ErrorToastOptions) => { - toasts.addError(error, { + (error: AppError, options: ErrorToastOptions) => + addError(error, { ...options, toastMessage: error.body.message, - }); - }, - [toasts] + }), + [addError] ); - const addError = useCallback( + const _addError = useCallback( (error: unknown, options: ErrorToastOptions) => { if (isAppError(error)) { - addAppError(error, options); + return addAppError(error, options); } else { if (error instanceof Error) { - toasts.addError(error, options); + return addError(error, options); } else { - toasts.addError(new Error(String(error)), options); + return addError(new Error(String(error)), options); } } }, - [addAppError, toasts] + [addAppError, addError] ); - return { ...toasts, addError }; + return { api: toasts, addError: _addError, addSuccess }; }; diff --git a/x-pack/plugins/security_solution/public/detections/components/value_lists_management_modal/modal.tsx b/x-pack/plugins/security_solution/public/detections/components/value_lists_management_modal/modal.tsx index 645c6750ba6ed..d7d4be6d951b8 100644 --- a/x-pack/plugins/security_solution/public/detections/components/value_lists_management_modal/modal.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/value_lists_management_modal/modal.tsx @@ -46,7 +46,7 @@ export const ValueListsModalComponent: React.FC = ({ const { start: findLists, ...lists } = useFindLists(); const { start: deleteList, result: deleteResult } = useDeleteList(); const [exportListId, setExportListId] = useState(); - const toasts = useAppToasts(); + const { addError, addSuccess } = useAppToasts(); const fetchLists = useCallback(() => { findLists({ cursor, http, pageIndex: pageIndex + 1, pageSize }); @@ -83,21 +83,21 @@ export const ValueListsModalComponent: React.FC = ({ const handleUploadError = useCallback( (error: Error) => { if (error.name !== 'AbortError') { - toasts.addError(error, { title: i18n.UPLOAD_ERROR }); + addError(error, { title: i18n.UPLOAD_ERROR }); } }, - [toasts] + [addError] ); const handleUploadSuccess = useCallback( (response: ListSchema) => { - toasts.addSuccess({ + addSuccess({ text: i18n.uploadSuccessMessage(response.name), title: i18n.UPLOAD_SUCCESS_TITLE, }); fetchLists(); }, // eslint-disable-next-line react-hooks/exhaustive-deps - [toasts] + [addSuccess] ); useEffect(() => { diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx index 7eb9f024422e2..4c0cd7fbc1db5 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx @@ -24,7 +24,7 @@ export const useListsIndex = (): UseListsIndexReturn => { const [error, setError] = useState(null); const { lists } = useKibana().services; const http = useHttp(); - const toasts = useAppToasts(); + const { addError, addSucess } = useAppToasts(); const { loading: readLoading, start: readListIndex, ...readListIndexState } = useReadListIndex(); const { loading: createLoading, @@ -76,19 +76,19 @@ export const useListsIndex = (): UseListsIndexReturn => { setIndexExists(false); } else { setError(err); - toasts.addError(err, { title: i18n.LISTS_INDEX_FETCH_FAILURE }); + addError(err, { title: i18n.LISTS_INDEX_FETCH_FAILURE }); } } - }, [readListIndexState.error, toasts]); + }, [addError, readListIndexState.error]); // handle create error useEffect(() => { const err = createListIndexState.error; if (err != null) { setError(err); - toasts.addError(err, { title: i18n.LISTS_INDEX_CREATE_FAILURE }); + addError(err, { title: i18n.LISTS_INDEX_CREATE_FAILURE }); } - }, [createListIndexState.error, toasts]); + }, [addError, createListIndexState.error]); return { createIndex, diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx index 27b640044d6b5..ca91efc5ceb3b 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx @@ -79,7 +79,7 @@ export const useListsPrivileges = (): UseListsPrivilegesReturn => { }); const { lists } = useKibana().services; const http = useHttp(); - const toasts = useAppToasts(); + const { addError, addSuccess } = useAppToasts(); const { loading, start: readListPrivileges, ...readState } = useReadListPrivileges(); const readPrivileges = useCallback(() => { @@ -121,11 +121,11 @@ export const useListsPrivileges = (): UseListsPrivilegesReturn => { const error = readState.error; if (error != null) { setState({ isAuthenticated: false, canManageIndex: false, canWriteIndex: false }); - toasts.addError(error, { + addError(error, { title: i18n.LISTS_PRIVILEGES_READ_FAILURE, }); } - }, [readState.error, toasts]); + }, [addError, readState.error]); return { loading, ...state }; }; From 59f36a9106d738d906d46ccb181214583f24999b Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 16 Jul 2020 19:03:18 -0500 Subject: [PATCH 09/11] Alert clipboard toasts through new Toasts service This fixes the z-index issue between modals and toasts. --- .../public/common/lib/clipboard/clipboard.tsx | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/security_solution/public/common/lib/clipboard/clipboard.tsx b/x-pack/plugins/security_solution/public/common/lib/clipboard/clipboard.tsx index fdb6ed130a525..75b7308ab61f1 100644 --- a/x-pack/plugins/security_solution/public/common/lib/clipboard/clipboard.tsx +++ b/x-pack/plugins/security_solution/public/common/lib/clipboard/clipboard.tsx @@ -4,13 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ -import { EuiGlobalToastListToast as Toast, EuiButtonIcon } from '@elastic/eui'; +import { EuiButtonIcon } from '@elastic/eui'; import copy from 'copy-to-clipboard'; import React from 'react'; -import uuid from 'uuid'; import * as i18n from './translations'; -import { useStateToaster } from '../../components/toasters'; +import { useAppToasts } from '../../hooks/use_app_toasts'; export type OnCopy = ({ content, @@ -20,17 +19,6 @@ export type OnCopy = ({ isSuccess: boolean; }) => void; -interface GetSuccessToastParams { - titleSummary?: string; -} - -const getSuccessToast = ({ titleSummary }: GetSuccessToastParams): Toast => ({ - id: `copy-success-${uuid.v4()}`, - color: 'success', - iconType: 'copyClipboard', - title: `${i18n.COPIED} ${titleSummary} ${i18n.TO_THE_CLIPBOARD}`, -}); - interface Props { children?: JSX.Element; content: string | number; @@ -40,7 +28,7 @@ interface Props { } export const Clipboard = ({ children, content, onCopy, titleSummary, toastLifeTimeMs }: Props) => { - const dispatchToaster = useStateToaster()[1]; + const { addSuccess } = useAppToasts(); const onClick = (event: React.MouseEvent) => { event.preventDefault(); event.stopPropagation(); @@ -52,10 +40,7 @@ export const Clipboard = ({ children, content, onCopy, titleSummary, toastLifeTi } if (isSuccess) { - dispatchToaster({ - type: 'addToaster', - toast: { toastLifeTimeMs, ...getSuccessToast({ titleSummary }) }, - }); + addSuccess(`${i18n.COPIED} ${titleSummary} ${i18n.TO_THE_CLIPBOARD}`, { toastLifeTimeMs }); } }; From 0800c29f7899535fa5b5f9e7d09ccb352e576765 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 16 Jul 2020 20:40:53 -0500 Subject: [PATCH 10/11] Fix type errors --- .../containers/detection_engine/lists/use_lists_index.tsx | 2 +- .../containers/detection_engine/lists/use_lists_privileges.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx index 4c0cd7fbc1db5..ee1316eb8a1fd 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_index.tsx @@ -24,7 +24,7 @@ export const useListsIndex = (): UseListsIndexReturn => { const [error, setError] = useState(null); const { lists } = useKibana().services; const http = useHttp(); - const { addError, addSucess } = useAppToasts(); + const { addError } = useAppToasts(); const { loading: readLoading, start: readListIndex, ...readListIndexState } = useReadListIndex(); const { loading: createLoading, diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx index ca91efc5ceb3b..f99f62b1948e6 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/lists/use_lists_privileges.tsx @@ -79,7 +79,7 @@ export const useListsPrivileges = (): UseListsPrivilegesReturn => { }); const { lists } = useKibana().services; const http = useHttp(); - const { addError, addSuccess } = useAppToasts(); + const { addError } = useAppToasts(); const { loading, start: readListPrivileges, ...readState } = useReadListPrivileges(); const readPrivileges = useCallback(() => { From 8597df79d2f396ccbcf5ed84d738e72b00b34f69 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 16 Jul 2020 20:45:20 -0500 Subject: [PATCH 11/11] Mock external dependency These tests now call out to the Notifications service (in a context) instead of our redux implementation. --- .../components/exceptions/viewer/exception_item/index.test.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x-pack/plugins/security_solution/public/common/components/exceptions/viewer/exception_item/index.test.tsx b/x-pack/plugins/security_solution/public/common/components/exceptions/viewer/exception_item/index.test.tsx index 0e2908fc34232..90752f9450e4c 100644 --- a/x-pack/plugins/security_solution/public/common/components/exceptions/viewer/exception_item/index.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/exceptions/viewer/exception_item/index.test.tsx @@ -13,6 +13,8 @@ import { ExceptionItem } from './'; import { getExceptionListItemSchemaMock } from '../../../../../../../lists/common/schemas/response/exception_list_item_schema.mock'; import { getCommentsArrayMock } from '../../../../../../../lists/common/schemas/types/comments.mock'; +jest.mock('../../../../lib/kibana'); + describe('ExceptionItem', () => { it('it renders ExceptionDetails and ExceptionEntries', () => { const exceptionItem = getExceptionListItemSchemaMock();