mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	improve feedback when strategy create/update fails
This commit is contained in:
		
							parent
							
								
									fc8b01cf44
								
							
						
					
					
						commit
						24b0358113
					
				| @ -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, | ||||
|  | ||||
| @ -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, | ||||
|  | ||||
| @ -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); | ||||
|  | ||||
| @ -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))); | ||||
|  | ||||
| @ -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)); | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user