Skip to content

Instantly share code, notes, and snippets.

@mkjiau
Last active July 20, 2017 16:25
Show Gist options
  • Save mkjiau/986ed8c819ea9fda66dcc4bf235a9eb5 to your computer and use it in GitHub Desktop.
Save mkjiau/986ed8c819ea9fda66dcc4bf235a9eb5 to your computer and use it in GitHub Desktop.
fetch(api.SEARCH_THE_ACCOUNT_ENDPOINT + "username=" + this.state.email, {
method: 'GET',
headers: {
'access_token': access_token
},
// body: formBody
}).then((response) => {
if (response.status >= 200 && response.status < 300) {
return response.json();
} else {
return Promise.reject({ status: response.status, body: response.body });
}
}).then((responseJson) => {
console.log("Fetch Response");
console.log(responseJson);
}).catch((error) => {
// error = { status: response.status, body: response.body }
if (error.status === 400 && error.body.status === 'TooManyPunch') {
// do something
} else if (error.status === 400 && error.body.status === 'UsernameFormatError') {
// do something
} else {
console.log('I also dont know what is this going on :(');
}
});
//--------------------------------------
fetch(api.SEARCH_THE_ACCOUNT_ENDPOINT + "username=" + this.state.email, {
method: 'GET',
headers: {
'access_token': access_token
},
// body: formBody
}).then((response) => {
if (response.status === 500) {
return Promise.reject(500);
} else {
return response.json().then(responseJson => {
return { status: response.status, json: responseJson };
});
}
}).then((response) => {
// response.status, response.json
if (response.status === 400 && response.json.status === 'TooManyPunch') {
// do something
} else if (response.status === 400 && response.json.status === 'UsernameFormatError') {
// do something
} else { // = status 2XX
// do normal thing
}
}).catch((error) => {
if (error === 500) {
// 500 Error Handling
} else {
// Unknown Error Handling, e.g. response.json() failed
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment