Skip to content

Commit b56cd41

Browse files
rylndXavierMelasticmachine
authored
[SIEM] Fix and consolidate handling of error responses in the client (#59438)
* Convert our manual throwing of TypeError to a custom Error Throwing a TypeError meant that our manual errors were indistinguishable from, say, trying to invoke a method on undefined. This adds a custom error, BadRequestError, that disambiguates that situation. * Present API Error messages to the user With Core's new HTTP client, an unsuccessful API call will raise an error containing the body of the response it received. In the case of SIEM endpoints, this will include a useful error message with potentially more specificity than e.g. 'Internal Server Error'. This adds a type predicate to check for such errors, and adds a handling case in our errorToToaster handler. If the error does not contain our SIEM-specific message, it will fall through as normal and the general error.message will be displayed in the toaster. * Remove unnecessary use of throwIfNotOk in our client API calls The new HTTP client raises an error on a 4xx or 5xx response, so there should not be a case where throwIfNotOk is actually going to throw an error. The established pattern on the frontend is to catch errors at the call site and handle them appropriately, so I'm mainly just verifying that these are caught where they're used, now. * Move errorToToaster and ToasterError to general location These were living in ML since that's where they originated. However, we have need of it (and already use it) elsewhere. The basic pattern for error handling on the frontend is: 1) API call throws error 2) caller catches error and dispatches a toast throwIfNotOk is meant to convert the error into a useful message in 1). We currently use both errorToToaster and displayErrorToast to display that in a toaster in 2) Now that errorToToaster handles a few different types of errors, and throwIfNotOk is going to be bypassed due to the new client behavior of throwing on error, we're going to start consolidating on: 1) Api call throws error 2) caller catches error and passes it to errorToToaster * Refactor Rules API functions to not use throwIfNotOk * Ensures that all callers of these methods properly catch errors * Updates error toasterification to use errorToToaster * Simplifies tests now that we mainly just invoke the http client and return the result. throwIfNotOk is not being used in the majority of cases, as the client raises an error and bypasses that call. The few cases this might break are where we return a 200 but have errors within the response. Whether throwIfNotOk handled this or not, I'll need a simpler helper to accomplish the same behavior. * Define a type for our BulkRule responses These can be an array of errors OR rules; typing it as such forces downstream to deal with both. enableRules was being handled correctly with the bucketing helper, and TS has confirmed the rest are as well. This obviates the need to raise from our API calls, as bulk errors are recoverable and we want to both a) continue on with any successful rules and b) handle the errors as necessary. This is highly dependent on the caller and so we can't/shouldn't handle it here. * Address case where bulk rules errors were not handled I'm not sure that we're ever using this non-dispatch version, but it was throwing a type error. Will bring it up in review. * Remove more throwIfNotOk uses from API calls These are unneeded as an error response will already throw an error to be handled at the call site. * Display an error toaster on newsfeed fetch failure * Remove dead code This was left over as a result of #56261 * Remove throwIfNotOk from case API calls Again, not needed because the client already throws. * Update use_get_tags for NP * Gets rid of throwIfNotOK usage * uses core http fetch * Remove throwIfNotOk from signals API * Remove throwIfNotOk This served the same purpose as errorToToaster, but in a less robust way. All usages have been replaced, so now we say goodbye. * Remove custom errors in favor of KibanaApiError and isApiError type predicate There was no functional difference between these two code paths, and removing these custom errors allowed us to delete a bunch of associated code as well.. * Fix test failures These were mainly related to my swapping any remaining fetch calls with the core router as good kibana denizens should :salute: * Replace use of core mocks with our simpler local ones This is enough to get our tests to pass. We can't use the core mocks for now since there are circular dependencies there, which breaks our build. * add signal api unit tests * privilege unit test api * Add unit tests on the signals container * Refactor signals API tests to use core mocks * Simplifies our mocking verbosity by leveraging core mocks * Simplifies test setup by isolating a reference to our fetch mock * Abstracts response structure to pure helper functions The try/catch tests had some false positives in that nothing would be asserted if the code did not throw an error. These proved to be masking a gap in coverage for our get/create signal index requests, which do not leverage `throwIfNotOk` but instead rely on the fetch to throw an error; once that behavior is verified we can update those tests to have our fetchMock throw errors, and we should be all set. * Simplify signals API tests now that the subjects do less We no longer re-throw errors, or parse the response, we just return the result of the client call. Simple! * Simplify API functions to use implict returns When possible. Also adds missing error-throwing documentation where necessary. * Revert "Display an error toaster on newsfeed fetch failure" This reverts commit 6421322. * Error property is readonly * Pull uuid generation into default argument value * Fix type predicate isApiError Uses has to properly inspect our errorish object. Turns out we have a 'message' property, not an 'error' property. * Fix test setup following modification of type predicate We need a message (via new Error), a body.message, and a body.status_code to satisfy isApiError. Co-authored-by: Xavier Mouligneau <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent eb533c8 commit b56cd41

File tree

72 files changed

+2142
-1221
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2142
-1221
lines changed

x-pack/legacy/plugins/siem/public/components/ml/anomaly/use_anomalies_table_data.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { InfluencerInput, Anomalies, CriteriaFields } from '../types';
1010
import { hasMlUserPermissions } from '../permissions/has_ml_user_permissions';
1111
import { MlCapabilitiesContext } from '../permissions/ml_capabilities_provider';
1212
import { useSiemJobs } from '../../ml_popover/hooks/use_siem_jobs';
13-
import { useStateToaster } from '../../toasters';
14-
import { errorToToaster } from '../api/error_to_toaster';
13+
import { useStateToaster, errorToToaster } from '../../toasters';
1514

1615
import * as i18n from './translations';
1716
import { useTimeZone, useUiSetting$ } from '../../../lib/kibana';

x-pack/legacy/plugins/siem/public/components/ml/api/anomalies_table_data.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import { Anomalies, InfluencerInput, CriteriaFields } from '../types';
8-
import { throwIfNotOk } from '../../../hooks/api/api';
98
import { KibanaServices } from '../../../lib/kibana';
109

1110
export interface Body {
@@ -22,17 +21,10 @@ export interface Body {
2221
}
2322

2423
export const anomaliesTableData = async (body: Body, signal: AbortSignal): Promise<Anomalies> => {
25-
const response = await KibanaServices.get().http.fetch<Anomalies>(
26-
'/api/ml/results/anomalies_table_data',
27-
{
28-
method: 'POST',
29-
body: JSON.stringify(body),
30-
asResponse: true,
31-
asSystemRequest: true,
32-
signal,
33-
}
34-
);
35-
36-
await throwIfNotOk(response.response);
37-
return response.body!;
24+
return KibanaServices.get().http.fetch<Anomalies>('/api/ml/results/anomalies_table_data', {
25+
method: 'POST',
26+
body: JSON.stringify(body),
27+
asSystemRequest: true,
28+
signal,
29+
});
3830
};

x-pack/legacy/plugins/siem/public/components/ml/api/error_to_toaster.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { has } from 'lodash/fp';
8+
9+
import { MlError } from '../types';
10+
11+
export interface MlStartJobError {
12+
error: MlError;
13+
started: boolean;
14+
}
15+
16+
// use the "in operator" and regular type guards to do a narrow once this issue is fixed below:
17+
// https://github.com/microsoft/TypeScript/issues/21732
18+
// Otherwise for now, has will work ok even though it casts 'unknown' to 'any'
19+
export const isMlStartJobError = (value: unknown): value is MlStartJobError =>
20+
has('error.msg', value) && has('error.response', value) && has('error.statusCode', value);

x-pack/legacy/plugins/siem/public/components/ml/api/get_ml_capabilities.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import { InfluencerInput, MlCapabilities } from '../types';
8-
import { throwIfNotOk } from '../../../hooks/api/api';
98
import { KibanaServices } from '../../../lib/kibana';
109

1110
export interface Body {
@@ -22,16 +21,9 @@ export interface Body {
2221
}
2322

2423
export const getMlCapabilities = async (signal: AbortSignal): Promise<MlCapabilities> => {
25-
const response = await KibanaServices.get().http.fetch<MlCapabilities>(
26-
'/api/ml/ml_capabilities',
27-
{
28-
method: 'GET',
29-
asResponse: true,
30-
asSystemRequest: true,
31-
signal,
32-
}
33-
);
34-
35-
await throwIfNotOk(response.response);
36-
return response.body!;
24+
return KibanaServices.get().http.fetch<MlCapabilities>('/api/ml/ml_capabilities', {
25+
method: 'GET',
26+
asSystemRequest: true,
27+
signal,
28+
});
3729
};

x-pack/legacy/plugins/siem/public/hooks/api/throw_if_not_ok.test.ts renamed to x-pack/legacy/plugins/siem/public/components/ml/api/throw_if_not_ok.test.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,21 @@
55
*/
66

77
import fetchMock from 'fetch-mock';
8+
9+
import { ToasterError } from '../../toasters';
10+
import { SetupMlResponse } from '../../ml_popover/types';
11+
import { isMlStartJobError } from './errors';
812
import {
9-
isMlStartJobError,
10-
MessageBody,
11-
parseJsonFromBody,
1213
throwIfErrorAttached,
1314
throwIfErrorAttachedToSetup,
14-
ToasterErrors,
1515
tryParseResponse,
1616
} from './throw_if_not_ok';
17-
import { SetupMlResponse } from '../../components/ml_popover/types';
1817

1918
describe('throw_if_not_ok', () => {
2019
afterEach(() => {
2120
fetchMock.reset();
2221
});
2322

24-
describe('#parseJsonFromBody', () => {
25-
test('parses a json from the body correctly', async () => {
26-
fetchMock.mock('http://example.com', {
27-
status: 500,
28-
body: {
29-
error: 'some error',
30-
statusCode: 500,
31-
message: 'I am a custom message',
32-
},
33-
});
34-
const response = await fetch('http://example.com');
35-
const expected: MessageBody = {
36-
error: 'some error',
37-
statusCode: 500,
38-
message: 'I am a custom message',
39-
};
40-
await expect(parseJsonFromBody(response)).resolves.toEqual(expected);
41-
});
42-
43-
test('returns null if the body does not exist', async () => {
44-
fetchMock.mock('http://example.com', { status: 500, body: 'some text' });
45-
const response = await fetch('http://example.com');
46-
await expect(parseJsonFromBody(response)).resolves.toEqual(null);
47-
});
48-
});
49-
5023
describe('#tryParseResponse', () => {
5124
test('It formats a JSON object', () => {
5225
const parsed = tryParseResponse(JSON.stringify({ hello: 'how are you?' }));
@@ -119,7 +92,7 @@ describe('throw_if_not_ok', () => {
11992
},
12093
};
12194
expect(() => throwIfErrorAttached(json, ['some-id'])).toThrow(
122-
new ToasterErrors(['some message'])
95+
new ToasterError(['some message'])
12396
);
12497
});
12598

x-pack/legacy/plugins/siem/public/hooks/api/throw_if_not_ok.ts renamed to x-pack/legacy/plugins/siem/public/components/ml/api/throw_if_not_ok.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,10 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { has } from 'lodash/fp';
8-
9-
import * as i18n from '../../components/ml/api/translations';
10-
import { MlError } from '../../components/ml/types';
11-
import { SetupMlResponse } from '../../components/ml_popover/types';
12-
13-
export { MessageBody, parseJsonFromBody } from '../../utils/api';
14-
15-
export interface MlStartJobError {
16-
error: MlError;
17-
started: boolean;
18-
}
19-
20-
export type ToasterErrorsType = Error & {
21-
messages: string[];
22-
};
23-
24-
export class ToasterErrors extends Error implements ToasterErrorsType {
25-
public messages: string[];
26-
27-
constructor(messages: string[]) {
28-
super(messages[0]);
29-
this.name = 'ToasterErrors';
30-
this.messages = messages;
31-
}
32-
}
7+
import * as i18n from './translations';
8+
import { ToasterError } from '../../toasters';
9+
import { SetupMlResponse } from '../../ml_popover/types';
10+
import { isMlStartJobError } from './errors';
3311

3412
export const tryParseResponse = (response: string): string => {
3513
try {
@@ -71,7 +49,7 @@ export const throwIfErrorAttachedToSetup = (
7149

7250
const errors = [...jobErrors, ...dataFeedErrors];
7351
if (errors.length > 0) {
74-
throw new ToasterErrors(errors);
52+
throw new ToasterError(errors);
7553
}
7654
};
7755

@@ -93,12 +71,6 @@ export const throwIfErrorAttached = (
9371
}
9472
}, []);
9573
if (errors.length > 0) {
96-
throw new ToasterErrors(errors);
74+
throw new ToasterError(errors);
9775
}
9876
};
99-
100-
// use the "in operator" and regular type guards to do a narrow once this issue is fixed below:
101-
// https://github.com/microsoft/TypeScript/issues/21732
102-
// Otherwise for now, has will work ok even though it casts 'unknown' to 'any'
103-
export const isMlStartJobError = (value: unknown): value is MlStartJobError =>
104-
has('error.msg', value) && has('error.response', value) && has('error.statusCode', value);

x-pack/legacy/plugins/siem/public/components/ml/permissions/ml_capabilities_provider.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import React, { useState, useEffect } from 'react';
99
import { MlCapabilities } from '../types';
1010
import { getMlCapabilities } from '../api/get_ml_capabilities';
1111
import { emptyMlCapabilities } from '../empty_ml_capabilities';
12-
import { errorToToaster } from '../api/error_to_toaster';
13-
import { useStateToaster } from '../../toasters';
12+
import { errorToToaster, useStateToaster } from '../../toasters';
1413

1514
import * as i18n from './translations';
1615

0 commit comments

Comments
 (0)