Skip to content

Commit

Permalink
Don't parse JSON if status code is not 2xx
Browse files Browse the repository at this point in the history
Fixes ethan7g#61

Prevents parsing body as JSON unless the status code is in the 2xx range to allow 4xx and 5xx errors to propagate without JSON parsing errors
  • Loading branch information
jdforsythe committed Mar 2, 2021
1 parent 5987ef1 commit 0b4b2b0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
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) {
res.coreRes.body = await res.json()
}

return res.coreRes
}
Expand Down

0 comments on commit 0b4b2b0

Please sign in to comment.