Created
May 24, 2019 02:16
-
-
Save Auraelius/9388c5c399f602b1568e63342ca3b7a2 to your computer and use it in GitHub Desktop.
More verbose error reporting (Thanks to Joshua) for Bookmark app fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
handleSubmit(e) { | |
e.preventDefault(); | |
const bookmark = (({title, url, description, rating}) => ({title, url, description, rating}))(this.state); | |
const url ='https://tf-ed-bookmarks-api.herokuapp.com/v3/bookmarks'; | |
const options = { | |
method: 'POST', | |
body: JSON.stringify(bookmark), | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer $2a$10$GJK5LhmFVC5ds9wFSucW4.VbHv8ktw1fv7cctl730JqNUbroOItKq" | |
} | |
}; | |
fetch(url, options) | |
.then(res => { | |
if(!res.ok) { | |
return res.json().then(data => { | |
console.log(`addBookmark: received ${res.status} ${res.statustext} response: `, data.message); | |
throw new Error('addBookmark: Something went wrong: ' + data.message); | |
}) | |
} | |
return res.json(); | |
}) | |
.then(data => { | |
this.setState({ | |
title: "", | |
url: "", | |
description: "", | |
rating: 1 | |
}); | |
this.props.handleAdd(bookmark); | |
}) | |
.catch(err => { | |
console.log('addBookmark: caught error:', err); | |
this.setState({ | |
error: err.message | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment