1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-26 13:48:33 +02:00

more error-cleanups

This commit is contained in:
ivaosthu 2016-10-26 17:58:22 +02:00 committed by Ivar Conradi Østhus
parent a8a6750a73
commit e4f826a5a2
5 changed files with 33 additions and 24 deletions

View File

@ -5,22 +5,20 @@ class ErrorComponent extends React.Component {
static propTypes () { static propTypes () {
return { return {
errors: PropTypes.array.isRequired, errors: PropTypes.array.isRequired,
showError: PropTypes.bool, muteError: PropTypes.func.isRequired,
muteErrors: PropTypes.func.isRequired,
}; };
} }
render () { render () {
const snackbarMsg = this.props.errors.join(', '); const showError = this.props.errors.length > 0;
const error = showError ? this.props.errors[0] : undefined;
return ( return (
<Snackbar <Snackbar
action="Dismiss" action="Dismiss"
active={this.props.showError} active={showError}
icon="question_answer" icon="question_answer"
label={snackbarMsg} label={error}
ref="snackbar" onClick={() => this.props.muteError(error)}
onClick={this.props.muteErrors}
onTimeout={this.props.muteErrors}
type="warning" type="warning"
/> />
); );

View File

@ -1,13 +1,14 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import ErrorComponent from './error-component'; import ErrorComponent from './error-component';
import { muteErrors } from '../../store/error-actions'; import { muteError } from '../../store/error-actions';
const mapDispatchToProps = { const mapDispatchToProps = {
muteErrors, muteError,
}; };
export default connect((state) => ({ const mapStateToProps = (state) => ({
errors: state.error.get('list').toArray(), errors: state.error.get('list').toArray(),
showError: state.error.get('showError'), });
}), mapDispatchToProps)(ErrorComponent);
export default connect(mapStateToProps, mapDispatchToProps)(ErrorComponent);

View File

@ -1,6 +1,9 @@
export const MUTE_ERRORS = 'MUTE_ERRORS'; export const MUTE_ERRORS = 'MUTE_ERRORS';
export const MUTE_ERROR = 'MUTE_ERROR';
export const muteErrors = () => ({ type: MUTE_ERRORS }); export const muteErrors = () => ({ type: MUTE_ERRORS });
export const muteError = (error) => ({ type: MUTE_ERROR, error });

View File

@ -1,9 +1,10 @@
import { List, Map as $Map } from 'immutable'; import { List, Map as $Map } from 'immutable';
import { MUTE_ERRORS } from './error-actions'; import { MUTE_ERROR } from './error-actions';
import { import {
ERROR_FETCH_FEATURE_TOGGLES, ERROR_FETCH_FEATURE_TOGGLES,
ERROR_CREATING_FEATURE_TOGGLE, ERROR_CREATING_FEATURE_TOGGLE,
ERROR_REMOVE_FEATURE_TOGGLE, ERROR_REMOVE_FEATURE_TOGGLE,
ERROR_UPDATE_FEATURE_TOGGLE,
} from './feature-actions'; } from './feature-actions';
const debug = require('debug')('unleash:error-store'); const debug = require('debug')('unleash:error-store');
@ -11,22 +12,26 @@ const debug = require('debug')('unleash:error-store');
function getInitState () { function getInitState () {
return new $Map({ return new $Map({
list: new List(), 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) => { const strategies = (state = getInitState(), action) => {
switch (action.type) { switch (action.type) {
case ERROR_CREATING_FEATURE_TOGGLE: case ERROR_CREATING_FEATURE_TOGGLE:
case ERROR_REMOVE_FEATURE_TOGGLE: case ERROR_REMOVE_FEATURE_TOGGLE:
case ERROR_FETCH_FEATURE_TOGGLES: case ERROR_FETCH_FEATURE_TOGGLES:
debug('Got error', action); case ERROR_UPDATE_FEATURE_TOGGLE:
return state return addErrorIfNotAlreadyInList(state, action.error.message);
.update('list', (list) => list.push(action.error.message)) case MUTE_ERROR:
.set('showError', true); return state.update('list', (list) => list.remove(list.indexOf(action.error)));
case MUTE_ERRORS:
debug('muting errors');
return state.set('showError', false);
default: default:
return state; return state;
} }

View File

@ -5,19 +5,21 @@ const headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
const defaultErrorMessage = 'Unexptected exception when talking to unleash-api';
function throwIfNotSuccess (response) { function throwIfNotSuccess (response) {
if (!response.ok) { if (!response.ok) {
if (response.status > 400 && response.status < 404) { if (response.status > 400 && response.status < 404) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
response.json().then(body => { 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); let error = new Error(errorMsg);
error.statusCode = response.status; error.statusCode = response.status;
reject(error); reject(error);
}); });
}); });
} else { } else {
return Promise.reject(new Error(response.statusText)); return Promise.reject(new Error(defaultErrorMessage));
} }
} }
return Promise.resolve(response); return Promise.resolve(response);