1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 09:35:10 +01:00

fix form error handling

This commit is contained in:
Tim Daubenschütz 2015-07-17 16:44:24 +02:00
parent 263a7b2676
commit 55058b8b34
2 changed files with 29 additions and 14 deletions

View File

@ -80,6 +80,7 @@ let Form = React.createClass({
this.setState({edited: false, submitted: false}); this.setState({edited: false, submitted: false});
}, },
handleError(err){ handleError(err){
console.log(err);
if (err.json) { if (err.json) {
for (var input in err.json.errors){ for (var input in err.json.errors){
if (this.refs && this.refs[input] && this.refs[input].state) { if (this.refs && this.refs[input] && this.refs[input].state) {

View File

@ -20,21 +20,36 @@ class Requests {
unpackResponse(response) { unpackResponse(response) {
if (response.status >= 500) { if (response.status >= 500) {
throw new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url); throw new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url);
} else if(response.status >= 400) {
throw new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url);
}
return response.text();
} }
customJSONparse(responseText) { return new Promise((resolve, reject) => {
response.text()
.then((responseText) => {
// If the responses' body does not contain any data, // If the responses' body does not contain any data,
// fetch will resolve responseText to the string 'None'. // fetch will resolve responseText to the string 'None'.
// If this is the case, we can not try to parse it as JSON. // If this is the case, we can not try to parse it as JSON.
if(responseText !== 'None') { if(responseText !== 'None') {
return JSON.parse(responseText); let body = JSON.parse(responseText);
if(body && body.errors) {
let error = new Error('Form Error');
error.json = body;
reject(error);
} else { } else {
return {}; resolve(body);
} }
} else {
if(response.status >= 400) {
reject(new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url));
} else {
resolve({});
}
}
}).catch((err) => {
reject(err);
});
});
} }
handleError(err) { handleError(err) {
@ -100,7 +115,6 @@ class Requests {
return fetch(url, merged) return fetch(url, merged)
.then(this.unpackResponse) .then(this.unpackResponse)
.then(this.customJSONparse)
.catch(this.handleError); .catch(this.handleError);
} }