Skip to content

Commit 01a2ec7

Browse files
committed
Show toast when an error ocurrs and add tests
1 parent ce00fcb commit 01a2ec7

File tree

6 files changed

+112
-8
lines changed

6 files changed

+112
-8
lines changed

x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/http_requests.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
6969
]);
7070
};
7171

72+
const setDeleteLogsCacheResponse = (response?: string, error?: ResponseError) => {
73+
const status = error ? error.statusCode || 400 : 200;
74+
const body = error ? error : response;
75+
server.respondWith('DELETE', `${API_BASE_PATH}/deprecation_logging/cache`, [
76+
status,
77+
{ 'Content-Type': 'application/json' },
78+
JSON.stringify(body),
79+
]);
80+
};
81+
7282
const setUpdateDeprecationLoggingResponse = (
7383
response?: DeprecationLoggingStatus,
7484
error?: ResponseError
@@ -169,6 +179,7 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
169179
setDeleteMlSnapshotResponse,
170180
setUpgradeMlSnapshotStatusResponse,
171181
setLoadDeprecationLogsCountResponse,
182+
setDeleteLogsCacheResponse,
172183
setStartReindexingResponse,
173184
setReindexStatusResponse,
174185
setLoadMlUpgradeModeResponse,

x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_logs_step/fix_logs_step.test.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ describe('Overview - Fix deprecation logs step', () => {
248248
describe('Step 3 - Resolve log issues', () => {
249249
beforeEach(async () => {
250250
httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(getLoggingResponse(true));
251+
httpRequestsMockHelpers.setDeleteLogsCacheResponse('ok');
251252
});
252253

253254
test('With deprecation warnings', async () => {
@@ -337,6 +338,47 @@ describe('Overview - Fix deprecation logs step', () => {
337338
expect(exists('noWarningsCallout')).toBe(true);
338339
});
339340

341+
test('Shows a toast if deleting cache fails', async () => {
342+
const error = {
343+
statusCode: 500,
344+
error: 'Internal server error',
345+
message: 'Internal server error',
346+
};
347+
348+
httpRequestsMockHelpers.setDeleteLogsCacheResponse(undefined, error);
349+
httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 10 });
350+
351+
const addDanger = jest.fn();
352+
await act(async () => {
353+
testBed = await setupOverviewPage({
354+
services: {
355+
core: {
356+
notifications: {
357+
toasts: {
358+
addDanger,
359+
},
360+
},
361+
},
362+
},
363+
});
364+
});
365+
366+
const { exists, actions, component } = testBed;
367+
368+
component.update();
369+
370+
httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0 });
371+
372+
await actions.clickResetButton();
373+
374+
// The toast should always be shown if the delete logs cache fails.
375+
expect(addDanger).toHaveBeenCalled();
376+
// Even though a stub for the API call getting the count was set to return 0,
377+
// given that the delete logs cache API call will fail the callout remains in
378+
// warning state.
379+
expect(exists('hasWarningsCallout')).toBe(true);
380+
});
381+
340382
describe('Poll for logs count', () => {
341383
beforeEach(async () => {
342384
jest.useFakeTimers();

x-pack/plugins/upgrade_assistant/public/application/components/overview/fix_logs_step/deprecations_count_checkpoint/deprecations_count_checkpoint.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const i18nTexts = {
4646
defaultMessage: 'Reset counter',
4747
}
4848
),
49+
errorToastTitle: i18n.translate('xpack.upgradeAssistant.overview.verifyChanges.errorToastTitle', {
50+
defaultMessage: 'Could not clear deprecation logs cache',
51+
}),
4952
};
5053

5154
interface Props {
@@ -60,7 +63,10 @@ export const DeprecationsCountCheckpoint: FunctionComponent<Props> = ({
6063
setHasNoDeprecationLogs,
6164
}) => {
6265
const {
63-
services: { api },
66+
services: {
67+
api,
68+
core: { notifications },
69+
},
6470
} = useAppContext();
6571
const { data, error, isLoading, resendRequest, isInitialRequest } =
6672
api.getDeprecationLogsCount(checkpoint);
@@ -72,9 +78,18 @@ export const DeprecationsCountCheckpoint: FunctionComponent<Props> = ({
7278
const calloutTestId = hasLogs ? 'hasWarningsCallout' : 'noWarningsCallout';
7379

7480
const onResetClick = async () => {
81+
const { error: deleteLogsCacheError } = await api.deleteDeprecationLogsCache();
82+
83+
if (deleteLogsCacheError) {
84+
notifications.toasts.addDanger({
85+
title: i18nTexts.errorToastTitle,
86+
text: deleteLogsCacheError.message.toString(),
87+
});
88+
return;
89+
}
90+
7591
const now = moment().toISOString();
7692
uiMetricService.trackUiMetric(METRIC_TYPE.CLICK, UIM_RESET_LOGS_COUNTER_CLICK);
77-
api.deleteDeprecationLogsCache();
7893
setCheckpoint(now);
7994
};
8095

x-pack/plugins/upgrade_assistant/public/application/lib/api.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,11 @@ export class ApiService {
9696
});
9797
}
9898

99-
public async deleteDeprecationLogsCache() {
100-
const result = await this.sendRequest({
101-
path: `${API_BASE_PATH}/deprecation_logging/deprecation_cache`,
99+
public deleteDeprecationLogsCache() {
100+
return this.sendRequest({
101+
path: `${API_BASE_PATH}/deprecation_logging/cache`,
102102
method: 'delete',
103103
});
104-
105-
return result;
106104
}
107105

108106
public async updateIndexSettings(indexName: string, settings: string[]) {

x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,42 @@ describe('deprecation logging API', () => {
176176
).rejects.toThrow('scary error!');
177177
});
178178
});
179+
180+
describe('DELETE /api/upgrade_assistant/deprecation_logging/cache', () => {
181+
it('returns ok if if the cache was deleted', async () => {
182+
(
183+
routeHandlerContextMock.core.elasticsearch.client.asCurrentUser.transport
184+
.request as jest.Mock
185+
).mockResolvedValue({
186+
body: 'ok',
187+
});
188+
189+
const resp = await routeDependencies.router.getHandler({
190+
method: 'delete',
191+
pathPattern: '/api/upgrade_assistant/deprecation_logging/cache',
192+
})(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory);
193+
194+
expect(resp.status).toEqual(200);
195+
expect(
196+
routeHandlerContextMock.core.elasticsearch.client.asCurrentUser.transport.request
197+
).toHaveBeenCalledWith({
198+
method: 'DELETE',
199+
path: '/_logging/deprecation_cache',
200+
});
201+
expect(resp.payload).toEqual('ok');
202+
});
203+
204+
it('returns an error if it throws', async () => {
205+
(
206+
routeHandlerContextMock.core.elasticsearch.client.asCurrentUser.transport
207+
.request as jest.Mock
208+
).mockRejectedValue(new Error('scary error!'));
209+
await expect(
210+
routeDependencies.router.getHandler({
211+
method: 'delete',
212+
pathPattern: '/api/upgrade_assistant/deprecation_logging/cache',
213+
})(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory)
214+
).rejects.toThrow('scary error!');
215+
});
216+
});
179217
});

x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export function registerDeprecationLoggingRoutes({
127127

128128
router.delete(
129129
{
130-
path: `${API_BASE_PATH}/deprecation_logging/deprecation_cache`,
130+
path: `${API_BASE_PATH}/deprecation_logging/cache`,
131131
validate: false,
132132
},
133133
versionCheckHandlerWrapper(

0 commit comments

Comments
 (0)