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

Conversation

jdforsythe
Copy link
Contributor

Fixes #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

Fixes ethanent#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
@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't parse JSON if response is a server error
2 participants