-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution][Detections] Better toast errors (#72205)
* Add new hook to wrap the toasts service When receiving error responses from our APIs, this gives us better toast messages. * Replace useToasts with useAppToasts in trivial case * 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. * 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. * Remove unnecessary conditions around creating our index If the index hook has an error needsIndex will not be true. * 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. * 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 * 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. * Alert clipboard toasts through new Toasts service This fixes the z-index issue between modals and toasts. * Fix type errors * Mock external dependency These tests now call out to the Notifications service (in a context) instead of our redux implementation.
- Loading branch information
Showing
14 changed files
with
211 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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; | ||
let addSuccessMock: jest.Mock; | ||
|
||
beforeEach(() => { | ||
addErrorMock = jest.fn(); | ||
addSuccessMock = jest.fn(); | ||
(useToasts as jest.Mock).mockImplementation(() => ({ | ||
addError: addErrorMock, | ||
addSuccess: addSuccessMock, | ||
})); | ||
}); | ||
|
||
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 AppError'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', | ||
}); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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, useRef } from 'react'; | ||
|
||
import { ErrorToastOptions, ToastsStart, Toast } from '../../../../../../src/core/public'; | ||
import { useToasts } from '../lib/kibana'; | ||
import { isAppError, AppError } from '../utils/api'; | ||
|
||
export type UseAppToasts = Pick<ToastsStart, 'addSuccess'> & { | ||
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) => | ||
addError(error, { | ||
...options, | ||
toastMessage: error.body.message, | ||
}), | ||
[addError] | ||
); | ||
|
||
const _addError = useCallback( | ||
(error: unknown, options: ErrorToastOptions) => { | ||
if (isAppError(error)) { | ||
return addAppError(error, options); | ||
} else { | ||
if (error instanceof Error) { | ||
return addError(error, options); | ||
} else { | ||
return addError(new Error(String(error)), options); | ||
} | ||
} | ||
}, | ||
[addAppError, addError] | ||
); | ||
|
||
return { api: toasts, addError: _addError, addSuccess }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.