From 24b0358113fe5eca21b05e3eeb3b91319d270ed5 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Sat, 17 Dec 2016 16:47:16 +0100 Subject: [PATCH] improve feedback when strategy create/update fails --- frontend/src/component/strategies/add-container.js | 2 +- .../src/component/strategies/edit-container.js | 2 +- frontend/src/data/helper.js | 9 ++++++++- frontend/src/store/error-store.js | 10 ++++++++++ frontend/src/store/strategy/actions.js | 14 ++++++++------ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/frontend/src/component/strategies/add-container.js b/frontend/src/component/strategies/add-container.js index 53cf00f46b..9b82ee8e7f 100644 --- a/frontend/src/component/strategies/add-container.js +++ b/frontend/src/component/strategies/add-container.js @@ -12,7 +12,7 @@ const prepare = (methods, dispatch) => { (e) => { e.preventDefault(); // clean - const parameters = input.parameters + const parameters = (input.parameters || []) .filter((name) => !!name) .map(({ name, diff --git a/frontend/src/component/strategies/edit-container.js b/frontend/src/component/strategies/edit-container.js index 752a327aad..00e77a1c3c 100644 --- a/frontend/src/component/strategies/edit-container.js +++ b/frontend/src/component/strategies/edit-container.js @@ -27,7 +27,7 @@ const prepare = (methods, dispatch) => { (e) => { e.preventDefault(); // clean - const parameters = input.parameters + const parameters = (input.parameters || []) .filter((name) => !!name) .map(({ name, diff --git a/frontend/src/data/helper.js b/frontend/src/data/helper.js index 4d396f379d..0775a699c6 100644 --- a/frontend/src/data/helper.js +++ b/frontend/src/data/helper.js @@ -1,11 +1,18 @@ const defaultErrorMessage = 'Unexptected exception when talking to unleash-api'; +function extractJoiMsg (body) { + return body.details.length > 0 ? body.details[0].message : defaultErrorMessage; +} +function extractLegacyMsg (body) { + return body && body.length > 0 ? body[0].msg : defaultErrorMessage; +} + export function throwIfNotSuccess (response) { if (!response.ok) { if (response.status > 399 && response.status < 404) { return new Promise((resolve, reject) => { response.json().then(body => { - const errorMsg = body && body.length > 0 ? body[0].msg : defaultErrorMessage; + const errorMsg = body && body.isJoi ? extractJoiMsg(body) : extractLegacyMsg(body); let error = new Error(errorMsg); error.statusCode = response.status; reject(error); diff --git a/frontend/src/store/error-store.js b/frontend/src/store/error-store.js index 129f8b6c37..fd9422ae00 100644 --- a/frontend/src/store/error-store.js +++ b/frontend/src/store/error-store.js @@ -7,6 +7,13 @@ import { ERROR_UPDATE_FEATURE_TOGGLE, } from './feature-actions'; +import { + ERROR_UPDATING_STRATEGY, + ERROR_CREATING_STRATEGY, + ERROR_RECEIVE_STRATEGIES, + +} from './strategy/actions'; + const debug = require('debug')('unleash:error-store'); function getInitState () { @@ -29,6 +36,9 @@ const strategies = (state = getInitState(), action) => { case ERROR_REMOVE_FEATURE_TOGGLE: case ERROR_FETCH_FEATURE_TOGGLES: case ERROR_UPDATE_FEATURE_TOGGLE: + case ERROR_UPDATING_STRATEGY: + case ERROR_CREATING_STRATEGY: + case ERROR_RECEIVE_STRATEGIES: return addErrorIfNotAlreadyInList(state, action.error.message); case MUTE_ERROR: return state.update('list', (list) => list.remove(list.indexOf(action.error))); diff --git a/frontend/src/store/strategy/actions.js b/frontend/src/store/strategy/actions.js index bd6269e62c..bed0dd40e9 100644 --- a/frontend/src/store/strategy/actions.js +++ b/frontend/src/store/strategy/actions.js @@ -21,11 +21,6 @@ const errorCreatingStrategy = (statusCode) => ({ statusCode, }); -const errorUpdatingStrategy = (statusCode) => ({ - type: ERROR_UPDATING_STRATEGY, - statusCode, -}); - const startRequest = () => ({ type: REQUEST_STRATEGIES }); @@ -43,6 +38,13 @@ const errorReceiveStrategies = (statusCode) => ({ const startUpdate = () => ({ type: START_UPDATE_STRATEGY }); +function dispatchAndThrow (dispatch, type) { + return (error) => { + dispatch({ type, error, receivedAt: Date.now() }); + throw error; + }; +} + export function fetchStrategies () { return dispatch => { dispatch(startRequest()); @@ -69,7 +71,7 @@ export function updateStrategy (strategy) { return api.update(strategy) .then(() => dispatch(updatedStrategy(strategy))) - .catch(error => dispatch(errorUpdatingStrategy(error))); + .catch(dispatchAndThrow(dispatch, ERROR_UPDATING_STRATEGY)); }; }