1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +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});
},
handleError(err){
console.log(err);
if (err.json) {
for (var input in err.json.errors){
if (this.refs && this.refs[input] && this.refs[input].state) {

View File

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