Skip to content

Commit

Permalink
Remove more throwIfNotOk uses from API calls
Browse files Browse the repository at this point in the history
These are unneeded as an error response will already throw an error to
be handled at the call site.
  • Loading branch information
rylnd committed Mar 5, 2020
1 parent 15edf2d commit 797b607
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { Anomalies, InfluencerInput, CriteriaFields } from '../types';
import { throwIfNotOk } from '../../../hooks/api/api';
import { KibanaServices } from '../../../lib/kibana';

export interface Body {
Expand All @@ -22,17 +21,10 @@ export interface Body {
}

export const anomaliesTableData = async (body: Body, signal: AbortSignal): Promise<Anomalies> => {
const response = await KibanaServices.get().http.fetch<Anomalies>(
'/api/ml/results/anomalies_table_data',
{
method: 'POST',
body: JSON.stringify(body),
asResponse: true,
asSystemRequest: true,
signal,
}
);

await throwIfNotOk(response.response);
return response.body!;
return KibanaServices.get().http.fetch<Anomalies>('/api/ml/results/anomalies_table_data', {
method: 'POST',
body: JSON.stringify(body),
asSystemRequest: true,
signal,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { InfluencerInput, MlCapabilities } from '../types';
import { throwIfNotOk } from '../../../hooks/api/api';
import { KibanaServices } from '../../../lib/kibana';

export interface Body {
Expand All @@ -22,16 +21,9 @@ export interface Body {
}

export const getMlCapabilities = async (signal: AbortSignal): Promise<MlCapabilities> => {
const response = await KibanaServices.get().http.fetch<MlCapabilities>(
'/api/ml/ml_capabilities',
{
method: 'GET',
asResponse: true,
asSystemRequest: true,
signal,
}
);

await throwIfNotOk(response.response);
return response.body!;
return KibanaServices.get().http.fetch<MlCapabilities>('/api/ml/ml_capabilities', {
method: 'GET',
asSystemRequest: true,
signal,
});
};
67 changes: 17 additions & 50 deletions x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
StopDatafeedResponse,
} from './types';
import { throwIfErrorAttached, throwIfErrorAttachedToSetup } from '../ml/api/throw_if_not_ok';
import { throwIfNotOk } from '../../hooks/api/api';
import { KibanaServices } from '../../lib/kibana';

/**
Expand All @@ -31,18 +30,14 @@ export const checkRecognizer = async ({
indexPatternName,
signal,
}: CheckRecognizerProps): Promise<RecognizerModule[]> => {
const response = await KibanaServices.get().http.fetch<RecognizerModule[]>(
return KibanaServices.get().http.fetch<RecognizerModule[]>(
`/api/ml/modules/recognize/${indexPatternName}`,
{
method: 'GET',
asResponse: true,
asSystemRequest: true,
signal,
}
);

await throwIfNotOk(response.response);
return response.body!;
};

/**
Expand All @@ -52,18 +47,11 @@ export const checkRecognizer = async ({
* @param signal to cancel request
*/
export const getModules = async ({ moduleId = '', signal }: GetModulesProps): Promise<Module[]> => {
const response = await KibanaServices.get().http.fetch<Module[]>(
`/api/ml/modules/get_module/${moduleId}`,
{
method: 'GET',
asResponse: true,
asSystemRequest: true,
signal,
}
);

await throwIfNotOk(response.response);
return response.body!;
return KibanaServices.get().http.fetch<Module[]>(`/api/ml/modules/get_module/${moduleId}`, {
method: 'GET',
asSystemRequest: true,
signal,
});
};

/**
Expand Down Expand Up @@ -93,16 +81,12 @@ export const setupMlJob = async ({
startDatafeed: false,
useDedicatedIndex: true,
}),
asResponse: true,
asSystemRequest: true,
}
);

await throwIfNotOk(response.response);
const json = response.body!;
throwIfErrorAttachedToSetup(json, jobIdErrorFilter);

return json;
throwIfErrorAttachedToSetup(response, jobIdErrorFilter);
return response;
};

/**
Expand All @@ -126,16 +110,12 @@ export const startDatafeeds = async ({
datafeedIds,
...(start !== 0 && { start }),
}),
asResponse: true,
asSystemRequest: true,
}
);

await throwIfNotOk(response.response);
const json = response.body!;
throwIfErrorAttached(json, datafeedIds);

return json;
throwIfErrorAttached(response, datafeedIds);
return response;
};

/**
Expand All @@ -155,14 +135,10 @@ export const stopDatafeeds = async ({
body: JSON.stringify({
datafeedIds,
}),
asResponse: true,
asSystemRequest: true,
}
);

await throwIfNotOk(stopDatafeedsResponse.response);
const stopDatafeedsResponseJson = stopDatafeedsResponse.body!;

const datafeedPrefix = 'datafeed-';
const closeJobsResponse = await KibanaServices.get().http.fetch<CloseJobsResponse>(
'/api/ml/jobs/close_jobs',
Expand All @@ -175,13 +151,11 @@ export const stopDatafeeds = async ({
: dataFeedId
),
}),
asResponse: true,
asSystemRequest: true,
}
);

await throwIfNotOk(closeJobsResponse.response);
return [stopDatafeedsResponseJson, closeJobsResponse.body!];
return [stopDatafeedsResponse, closeJobsResponse];
};

/**
Expand All @@ -193,17 +167,10 @@ export const stopDatafeeds = async ({
* @param signal to cancel request
*/
export const getJobsSummary = async (signal: AbortSignal): Promise<JobSummary[]> => {
const response = await KibanaServices.get().http.fetch<JobSummary[]>(
'/api/ml/jobs/jobs_summary',
{
method: 'POST',
body: JSON.stringify({}),
asResponse: true,
asSystemRequest: true,
signal,
}
);

await throwIfNotOk(response.response);
return response.body!;
return KibanaServices.get().http.fetch<JobSummary[]>('/api/ml/jobs/jobs_summary', {
method: 'POST',
body: JSON.stringify({}),
asSystemRequest: true,
signal,
});
};

0 comments on commit 797b607

Please sign in to comment.