mirror of
https://github.com/ascribe/onion.git
synced 2024-11-15 01:25:17 +01:00
fix form error handling
This commit is contained in:
parent
263a7b2676
commit
55058b8b34
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user