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

fix: handle the case of invalid json gracefully #1581

Merged
merged 5 commits into from
Sep 7, 2020

Conversation

weyert
Copy link
Contributor

@weyert weyert commented Sep 4, 2020

This changes the behaviour how invalid json is handled in the /decide-endpoint instead of failing hard by throwing a 500 internal server error it instead will throw a 400 Bad Request error instead to make the Developer Experience better

fixes #1576

Changes

Please describe.
If this affects the front-end, include screenshots.

Checklist

  • All querysets/queries filter by Team (if this PR affects any querysets/queries)
  • Backend tests (if this PR affects the backend)
  • Cypress E2E tests (if this PR affects the front and/or backend)

This changes the behaviour how invalid json is handled in the `/decide`-endpoint instead of failing hard by throwing a 500 internal server error it instead will throw a 400 Bad Request error instead to make the Developer Experience better

fixes PostHog#1576
@Twixes Twixes mentioned this pull request Sep 4, 2020
3 tasks
@weyert
Copy link
Contributor Author

weyert commented Sep 5, 2020

Originally I want to use a custom exception handler and return 400 when JSONError exception is thrown but I am seem to unable to get my exception handler to get called. The main reason for this approach to change the behaviour for all API routes.

@yakkomajuri
Copy link
Contributor

Thanks for this @weyert! I'll be giving it a look when I get the chance early next week! I'll look into the tests and make sure the changes fit nicely with #1553

@yakkomajuri
Copy link
Contributor

Ok so this right now works but is not the way I want to handle it because it adds redundancy through calling _load_data twice. The approach @weyert is following here is drawn from our /capture endpoint, but I've now realized that the implementation /capture uses doesn't actually work to detect all malformed JSON. I'm working on it.

@yakkomajuri yakkomajuri self-requested a review September 7, 2020 11:30
@yakkomajuri
Copy link
Contributor

Ha, I've been hopping around timezones so my GitHub posts are happening "in the future".

"Yakko self-requested a review in 1h"

@yakkomajuri
Copy link
Contributor

Ok so @weyert had mentioned that we should see if there are any other endpoints that could benefit from this approach. I think /capture potentially could. However, I'll merge this as it is somewhat "blocking" me on #1553 and then see with another PR the other endpoints this could be applied to.

Thanks for this @weyert! I definitely agree we should be throwing as few 500s as possible.

@yakkomajuri yakkomajuri marked this pull request as ready for review September 7, 2020 11:40
@yakkomajuri yakkomajuri merged commit 832b55f into PostHog:master Sep 7, 2020
@weyert
Copy link
Contributor Author

weyert commented Sep 7, 2020

Thank you @yakkomajuri for looking and accepting my pull request :D

@weyert weyert deleted the 1576-fail-gracefully branch September 7, 2020 14:29
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.

Invalid JSON causes internal server error
3 participants