-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SIEM] Add toaster logic for Machine Learning #41401
Changes from 9 commits
6383e68
f82abe5
7d2e6bd
6fb1cf1
6e0c1eb
27ce944
0f501b8
54a0f70
d991a22
b191778
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
|
||
export const SIEM_TABLE_FETCH_FAILURE = i18n.translate( | ||
'xpack.siem.components.ml.anomaly.errors.anomaliesTableFetchFailureTitle', | ||
{ | ||
defaultMessage: 'Anomalies table fetch failure', | ||
} | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* 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 { isAnError, isToasterError, errorToToaster } from './error_to_toaster'; | ||
import { ToasterErrors } from './throw_if_not_ok'; | ||
|
||
describe('error_to_toaster', () => { | ||
let dispatchToaster = jest.fn(); | ||
|
||
beforeEach(() => { | ||
dispatchToaster = jest.fn(); | ||
}); | ||
|
||
describe('#errorToToaster', () => { | ||
test('adds a ToastError given multiple toaster errors', () => { | ||
const error = new ToasterErrors(['some error 1', 'some error 2']); | ||
errorToToaster({ id: 'some-made-up-id', title: 'some title', error, dispatchToaster }); | ||
expect(dispatchToaster.mock.calls[0]).toEqual([ | ||
{ | ||
toast: { | ||
color: 'danger', | ||
errors: ['some error 1', 'some error 2'], | ||
iconType: 'alert', | ||
id: 'some-made-up-id', | ||
title: 'some title', | ||
}, | ||
type: 'addToaster', | ||
}, | ||
]); | ||
}); | ||
|
||
test('adds a ToastError given a single toaster errors', () => { | ||
const error = new ToasterErrors(['some error 1']); | ||
errorToToaster({ id: 'some-made-up-id', title: 'some title', error, dispatchToaster }); | ||
expect(dispatchToaster.mock.calls[0]).toEqual([ | ||
{ | ||
toast: { | ||
color: 'danger', | ||
errors: ['some error 1'], | ||
iconType: 'alert', | ||
id: 'some-made-up-id', | ||
title: 'some title', | ||
}, | ||
type: 'addToaster', | ||
}, | ||
]); | ||
}); | ||
|
||
test('adds a regular Error given a single error', () => { | ||
const error = new Error('some error 1'); | ||
errorToToaster({ id: 'some-made-up-id', title: 'some title', error, dispatchToaster }); | ||
expect(dispatchToaster.mock.calls[0]).toEqual([ | ||
{ | ||
toast: { | ||
color: 'danger', | ||
errors: ['some error 1'], | ||
iconType: 'alert', | ||
id: 'some-made-up-id', | ||
title: 'some title', | ||
}, | ||
type: 'addToaster', | ||
}, | ||
]); | ||
}); | ||
|
||
test('adds a generic Network Error given a non Error object such as a string', () => { | ||
const error = 'terrible string'; | ||
errorToToaster({ id: 'some-made-up-id', title: 'some title', error, dispatchToaster }); | ||
expect(dispatchToaster.mock.calls[0]).toEqual([ | ||
{ | ||
toast: { | ||
color: 'danger', | ||
errors: ['Network Error'], | ||
iconType: 'alert', | ||
id: 'some-made-up-id', | ||
title: 'some title', | ||
}, | ||
type: 'addToaster', | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('#isAnError', () => { | ||
test('returns true if given an error object', () => { | ||
const error = new Error('some error'); | ||
expect(isAnError(error)).toEqual(true); | ||
}); | ||
|
||
test('returns false if given a regular object', () => { | ||
expect(isAnError({})).toEqual(false); | ||
}); | ||
|
||
test('returns false if given a string', () => { | ||
expect(isAnError('som string')).toEqual(false); | ||
}); | ||
|
||
test('returns true if given a toaster error', () => { | ||
const error = new ToasterErrors(['some error']); | ||
expect(isAnError(error)).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('#isToasterError', () => { | ||
test('returns true if given a ToasterErrors object', () => { | ||
const error = new ToasterErrors(['some error']); | ||
expect(isToasterError(error)).toEqual(true); | ||
}); | ||
|
||
test('returns false if given a regular object', () => { | ||
expect(isToasterError({})).toEqual(false); | ||
}); | ||
|
||
test('returns false if given a string', () => { | ||
expect(isToasterError('som string')).toEqual(false); | ||
}); | ||
|
||
test('returns false if given a regular error', () => { | ||
const error = new Error('some error'); | ||
expect(isToasterError(error)).toEqual(false); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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 { isError } from 'lodash/fp'; | ||
import uuid from 'uuid'; | ||
import { ActionToaster, AppToast } from '../../toasters'; | ||
import { ToasterErrorsType, ToasterErrors } from './throw_if_not_ok'; | ||
|
||
export type ErrorToToasterArgs = Partial<AppToast> & { | ||
error: unknown; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ on the |
||
dispatchToaster: React.Dispatch<ActionToaster>; | ||
}; | ||
|
||
export const errorToToaster = ({ | ||
id = uuid.v4(), | ||
title, | ||
error, | ||
color = 'danger', | ||
iconType = 'alert', | ||
dispatchToaster, | ||
}: ErrorToToasterArgs) => { | ||
if (isToasterError(error)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, I think that you can simplify it like that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appreciate it, I will try to work it into a follow up PR 🤞 here soon. Thank you for looking at my PR Xavier and giving me feedback. |
||
const toast: AppToast = { | ||
id, | ||
title, | ||
color, | ||
iconType, | ||
errors: error.messages, | ||
}; | ||
dispatchToaster({ | ||
type: 'addToaster', | ||
toast, | ||
}); | ||
} else if (isAnError(error)) { | ||
const toast: AppToast = { | ||
id, | ||
title, | ||
color, | ||
iconType, | ||
errors: [error.message], | ||
}; | ||
dispatchToaster({ | ||
type: 'addToaster', | ||
toast, | ||
}); | ||
} else { | ||
const toast: AppToast = { | ||
id, | ||
title, | ||
color, | ||
iconType, | ||
errors: ['Network Error'], | ||
}; | ||
dispatchToaster({ | ||
type: 'addToaster', | ||
toast, | ||
}); | ||
} | ||
}; | ||
|
||
export const isAnError = (error: unknown): error is Error => isError(error); | ||
|
||
export const isToasterError = (error: unknown): error is ToasterErrorsType => | ||
error instanceof ToasterErrors; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit -I am used to see it the other way around like
const dispatchToaster = useStateToaster()[1];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing wrong with that way since it is valid ts/js, but in the all the videos for React Hooks, including the introduction of them, they use array destructoring which is the reasoning for my choice.
Let me know if I'm not seeing something with the tutorials or others that have hooks which use array indexes over destructoring as I am curious.