Skip to content
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

[7.x] [Uptime]Update fetch effect failed action handling (#60742) #60872

Merged
merged 1 commit into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 { call, put } from 'redux-saga/effects';
import { fetchEffectFactory } from '../fetch_effect';
import { indexStatusAction } from '../../actions';

describe('fetch saga effect factory', () => {
const asyncAction = indexStatusAction;
const calledAction = asyncAction.get();
let fetchEffect;

it('works with success workflow', () => {
const indexStatusResult = { indexExists: true, docCount: 2712532 };
const fetchStatus = async () => {
return { indexExists: true, docCount: 2712532 };
};
fetchEffect = fetchEffectFactory(
fetchStatus,
asyncAction.success,
asyncAction.fail
)(calledAction);
let next = fetchEffect.next();

expect(next.value).toEqual(call(fetchStatus, calledAction.payload));

const successResult = put(asyncAction.success(indexStatusResult));

next = fetchEffect.next(indexStatusResult);

expect(next.value).toEqual(successResult);
});

it('works with error workflow', () => {
const indexStatusResultError = new Error('no heartbeat index found');
const fetchStatus = async () => {
return indexStatusResultError;
};
fetchEffect = fetchEffectFactory(
fetchStatus,
asyncAction.success,
asyncAction.fail
)(calledAction);
let next = fetchEffect.next();

expect(next.value).toEqual(call(fetchStatus, calledAction.payload));

const errorResult = put(asyncAction.fail(indexStatusResultError));

next = fetchEffect.next(indexStatusResultError);

expect(next.value).toEqual(errorResult);
});

it('works with throw error workflow', () => {
const unExpectedError = new Error('no url found, so throw error');
const fetchStatus = async () => {
return await fetch('/some/url');
};
fetchEffect = fetchEffectFactory(
fetchStatus,
asyncAction.success,
asyncAction.fail
)(calledAction);
let next = fetchEffect.next();

expect(next.value).toEqual(call(fetchStatus, calledAction.payload));

const unexpectedErrorResult = put(asyncAction.fail(unExpectedError));

next = fetchEffect.next(unExpectedError);

expect(next.value).toEqual(unexpectedErrorResult);
});
});
24 changes: 14 additions & 10 deletions x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ export function fetchEffectFactory<T, R, S, F>(
fail: (error: Error) => Action<F>
) {
return function*(action: Action<T>) {
const {
payload: { ...params },
} = action;
const response = yield call(fetch, params);
if (response instanceof Error) {
// eslint-disable-next-line no-console
console.error(response);
try {
const response = yield call(fetch, action.payload);

if (response instanceof Error) {
// eslint-disable-next-line no-console
console.error(response);

yield put(fail(response));
} else {
yield put(success(response));
yield put(fail(response));
} else {
yield put(success(response));
}
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
yield put(fail(error));
}
};
}