From e4f826a5a26e5220144935ae34494b33fe8e723a Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Wed, 26 Oct 2016 17:58:22 +0200 Subject: [PATCH] more error-cleanups --- .../src/component/error/error-component.jsx | 14 +++++------ .../src/component/error/error-container.jsx | 11 +++++---- .../src/store/error-actions.js | 3 +++ .../src/store/error-store.js | 23 +++++++++++-------- .../src/store/feature-api.js | 6 +++-- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/packages/unleash-frontend-next/src/component/error/error-component.jsx b/packages/unleash-frontend-next/src/component/error/error-component.jsx index 9bd1d64017..22e3d6faf2 100644 --- a/packages/unleash-frontend-next/src/component/error/error-component.jsx +++ b/packages/unleash-frontend-next/src/component/error/error-component.jsx @@ -5,22 +5,20 @@ class ErrorComponent extends React.Component { static propTypes () { return { errors: PropTypes.array.isRequired, - showError: PropTypes.bool, - muteErrors: PropTypes.func.isRequired, + muteError: PropTypes.func.isRequired, }; } render () { - const snackbarMsg = this.props.errors.join(', '); + const showError = this.props.errors.length > 0; + const error = showError ? this.props.errors[0] : undefined; return ( this.props.muteError(error)} type="warning" /> ); diff --git a/packages/unleash-frontend-next/src/component/error/error-container.jsx b/packages/unleash-frontend-next/src/component/error/error-container.jsx index 2cc154e1ba..04dd974fd0 100644 --- a/packages/unleash-frontend-next/src/component/error/error-container.jsx +++ b/packages/unleash-frontend-next/src/component/error/error-container.jsx @@ -1,13 +1,14 @@ import { connect } from 'react-redux'; import ErrorComponent from './error-component'; -import { muteErrors } from '../../store/error-actions'; +import { muteError } from '../../store/error-actions'; const mapDispatchToProps = { - muteErrors, + muteError, }; -export default connect((state) => ({ +const mapStateToProps = (state) => ({ errors: state.error.get('list').toArray(), - showError: state.error.get('showError'), -}), mapDispatchToProps)(ErrorComponent); +}); + +export default connect(mapStateToProps, mapDispatchToProps)(ErrorComponent); diff --git a/packages/unleash-frontend-next/src/store/error-actions.js b/packages/unleash-frontend-next/src/store/error-actions.js index 1eafce4915..bd47b2d590 100644 --- a/packages/unleash-frontend-next/src/store/error-actions.js +++ b/packages/unleash-frontend-next/src/store/error-actions.js @@ -1,6 +1,9 @@ export const MUTE_ERRORS = 'MUTE_ERRORS'; +export const MUTE_ERROR = 'MUTE_ERROR'; export const muteErrors = () => ({ type: MUTE_ERRORS }); +export const muteError = (error) => ({ type: MUTE_ERROR, error }); + diff --git a/packages/unleash-frontend-next/src/store/error-store.js b/packages/unleash-frontend-next/src/store/error-store.js index eab6f1492e..129f8b6c37 100644 --- a/packages/unleash-frontend-next/src/store/error-store.js +++ b/packages/unleash-frontend-next/src/store/error-store.js @@ -1,9 +1,10 @@ import { List, Map as $Map } from 'immutable'; -import { MUTE_ERRORS } from './error-actions'; +import { MUTE_ERROR } from './error-actions'; import { ERROR_FETCH_FEATURE_TOGGLES, ERROR_CREATING_FEATURE_TOGGLE, ERROR_REMOVE_FEATURE_TOGGLE, + ERROR_UPDATE_FEATURE_TOGGLE, } from './feature-actions'; const debug = require('debug')('unleash:error-store'); @@ -11,22 +12,26 @@ const debug = require('debug')('unleash:error-store'); function getInitState () { return new $Map({ list: new List(), - showError: false, }); } +function addErrorIfNotAlreadyInList (state, error) { + debug('Got error', error); + if (state.get('list').indexOf(error) < 0) { + return state.update('list', (list) => list.push(error)); + } + return state; +} + const strategies = (state = getInitState(), action) => { switch (action.type) { case ERROR_CREATING_FEATURE_TOGGLE: case ERROR_REMOVE_FEATURE_TOGGLE: case ERROR_FETCH_FEATURE_TOGGLES: - debug('Got error', action); - return state - .update('list', (list) => list.push(action.error.message)) - .set('showError', true); - case MUTE_ERRORS: - debug('muting errors'); - return state.set('showError', false); + case ERROR_UPDATE_FEATURE_TOGGLE: + return addErrorIfNotAlreadyInList(state, action.error.message); + case MUTE_ERROR: + return state.update('list', (list) => list.remove(list.indexOf(action.error))); default: return state; } diff --git a/packages/unleash-frontend-next/src/store/feature-api.js b/packages/unleash-frontend-next/src/store/feature-api.js index 2716f9fc4c..13e44b430e 100644 --- a/packages/unleash-frontend-next/src/store/feature-api.js +++ b/packages/unleash-frontend-next/src/store/feature-api.js @@ -5,19 +5,21 @@ const headers = { 'Content-Type': 'application/json', }; +const defaultErrorMessage = 'Unexptected exception when talking to unleash-api'; + function throwIfNotSuccess (response) { if (!response.ok) { if (response.status > 400 && response.status < 404) { return new Promise((resolve, reject) => { response.json().then(body => { - const errorMsg = body && body.length > 0 ? body[0].msg : 'API call failed'; + const errorMsg = body && body.length > 0 ? body[0].msg : defaultErrorMessage; let error = new Error(errorMsg); error.statusCode = response.status; reject(error); }); }); } else { - return Promise.reject(new Error(response.statusText)); + return Promise.reject(new Error(defaultErrorMessage)); } } return Promise.resolve(response);