Skip to content

Commit a3b8ac0

Browse files
committed
[Uptime]Update fetch effect failed action handling (#60742)
* update fetch effect * added test * update type
1 parent 4057f56 commit a3b8ac0

File tree

2 files changed

+92
-10
lines changed

2 files changed

+92
-10
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 { call, put } from 'redux-saga/effects';
8+
import { fetchEffectFactory } from '../fetch_effect';
9+
import { indexStatusAction } from '../../actions';
10+
11+
describe('fetch saga effect factory', () => {
12+
const asyncAction = indexStatusAction;
13+
const calledAction = asyncAction.get();
14+
let fetchEffect;
15+
16+
it('works with success workflow', () => {
17+
const indexStatusResult = { indexExists: true, docCount: 2712532 };
18+
const fetchStatus = async () => {
19+
return { indexExists: true, docCount: 2712532 };
20+
};
21+
fetchEffect = fetchEffectFactory(
22+
fetchStatus,
23+
asyncAction.success,
24+
asyncAction.fail
25+
)(calledAction);
26+
let next = fetchEffect.next();
27+
28+
expect(next.value).toEqual(call(fetchStatus, calledAction.payload));
29+
30+
const successResult = put(asyncAction.success(indexStatusResult));
31+
32+
next = fetchEffect.next(indexStatusResult);
33+
34+
expect(next.value).toEqual(successResult);
35+
});
36+
37+
it('works with error workflow', () => {
38+
const indexStatusResultError = new Error('no heartbeat index found');
39+
const fetchStatus = async () => {
40+
return indexStatusResultError;
41+
};
42+
fetchEffect = fetchEffectFactory(
43+
fetchStatus,
44+
asyncAction.success,
45+
asyncAction.fail
46+
)(calledAction);
47+
let next = fetchEffect.next();
48+
49+
expect(next.value).toEqual(call(fetchStatus, calledAction.payload));
50+
51+
const errorResult = put(asyncAction.fail(indexStatusResultError));
52+
53+
next = fetchEffect.next(indexStatusResultError);
54+
55+
expect(next.value).toEqual(errorResult);
56+
});
57+
58+
it('works with throw error workflow', () => {
59+
const unExpectedError = new Error('no url found, so throw error');
60+
const fetchStatus = async () => {
61+
return await fetch('/some/url');
62+
};
63+
fetchEffect = fetchEffectFactory(
64+
fetchStatus,
65+
asyncAction.success,
66+
asyncAction.fail
67+
)(calledAction);
68+
let next = fetchEffect.next();
69+
70+
expect(next.value).toEqual(call(fetchStatus, calledAction.payload));
71+
72+
const unexpectedErrorResult = put(asyncAction.fail(unExpectedError));
73+
74+
next = fetchEffect.next(unExpectedError);
75+
76+
expect(next.value).toEqual(unexpectedErrorResult);
77+
});
78+
});

x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ export function fetchEffectFactory<T, R, S, F>(
2424
fail: (error: Error) => Action<F>
2525
) {
2626
return function*(action: Action<T>) {
27-
const {
28-
payload: { ...params },
29-
} = action;
30-
const response = yield call(fetch, params);
31-
if (response instanceof Error) {
32-
// eslint-disable-next-line no-console
33-
console.error(response);
27+
try {
28+
const response = yield call(fetch, action.payload);
29+
30+
if (response instanceof Error) {
31+
// eslint-disable-next-line no-console
32+
console.error(response);
3433

35-
yield put(fail(response));
36-
} else {
37-
yield put(success(response));
34+
yield put(fail(response));
35+
} else {
36+
yield put(success(response));
37+
}
38+
} catch (error) {
39+
// eslint-disable-next-line no-console
40+
console.error(error);
41+
yield put(fail(error));
3842
}
3943
};
4044
}

0 commit comments

Comments
 (0)