Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't parse JSON if status code is not 2xx #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Mar 01 2021 21:08:53 GMT-0500 (EST)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Mar 01 2021 21:35:41 GMT-0500 (EST)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Mar 01 2021 21:08:53 GMT-0500 (EST)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Mar 01 2021 21:35:41 GMT-0500 (EST)
</footer>

<script> prettyPrint(); </script>
Expand Down
7 changes: 5 additions & 2 deletions docs/phin.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ <h1 class="page-title">Source: phin.js</h1>

if (opts.parse) {
if (opts.parse === 'json') {
res.coreRes.body = await res.json()
// prevent JSON parsing errors on 4xx or 5xx status codes by not parsing unless the code is 2xx
if (res.statusCode > 199 &amp;&amp; res.statusCode &lt; 300) {
res.coreRes.body = await res.json()
}

return res.coreRes
}
Expand Down Expand Up @@ -159,7 +162,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Mar 01 2021 21:08:53 GMT-0500 (EST)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Mar 01 2021 21:35:41 GMT-0500 (EST)
</footer>

<script> prettyPrint(); </script>
Expand Down
5 changes: 4 additions & 1 deletion lib/phin.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ const phin = async (opts) => {

if (opts.parse) {
if (opts.parse === 'json') {
res.coreRes.body = await res.json()
// prevent JSON parsing errors on 4xx or 5xx status codes by not parsing unless the code is 2xx
if (res.statusCode > 199 && res.statusCode < 300) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me res.statusCode >= 200 is more intuitive.

res.coreRes.body = await res.json()
}

return res.coreRes
}
Expand Down