From 87c1ee7516a8d4fffd409f2d758c358bebc21418 Mon Sep 17 00:00:00 2001 From: Jari Bakken Date: Mon, 10 Nov 2014 14:55:56 +0100 Subject: [PATCH] Improve error messages. --- .../feature/FeatureTogglesComponent.jsx | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/public/js/components/feature/FeatureTogglesComponent.jsx b/public/js/components/feature/FeatureTogglesComponent.jsx index 1089568ef5..572612f3b4 100644 --- a/public/js/components/feature/FeatureTogglesComponent.jsx +++ b/public/js/components/feature/FeatureTogglesComponent.jsx @@ -34,7 +34,14 @@ var FeatureTogglesComponent = React.createClass({ }, handleError: function (error) { - this.state.errors.push(error); + if (this.isClientError(error)) { + // TODO: catch SyntaxError + var errors = JSON.parse(error.responseText) + errors.forEach(function(e) { this.state.errors.push(e.msg); }.bind(this)) + } else { + this.state.errors.push(error); + } + this.forceUpdate(); }, @@ -81,6 +88,22 @@ var FeatureTogglesComponent = React.createClass({ this.setState({errors: []}); }, + isClientError: function(error) { + try { + return error.status >= 400 && + error.status < 500 && + JSON.parse(error.responseText); + } catch (e) { + if (e instanceof SyntaxError) { + // fall through; + } else { + throw e; + } + } + + return false; + }, + render: function() { return (