django-cookie-consent icon indicating copy to clipboard operation
django-cookie-consent copied to clipboard

Catch Unexpected Errors Part 1

Open 9mido opened this issue 5 years ago • 2 comments

This helps with #26. Needed a way to deal with unexpected errors that come up. I couldn't figure out a better way so maybe someone else could. I put more details at the bottom of the issue of what caused this. This error wasn't just tied to django-honeypot. For example, making a session variable and then deleting it from the database caused the same type of problem. If you want me to share the code for that problem with the session variable and how it creates the same error, let me know.

9mido avatar Jul 27 '20 21:07 9mido

Hi @9mido and thanks for pull request. I do not think it would be good idea, to swallow errors. We should find where unexpected condition comes from and why. If you can reproduce this issue, I recommend to put some __import__('pdb').set_trace() statements just before line with error and inspect variables.

bmihelac avatar Jul 28 '20 07:07 bmihelac

@bmihelac This is the current behavior when it works:

(Pdb) request.COOKIES.get(settings.COOKIE_CONSENT_NAME)
'Stripe=2020-04-01T01:59:44.145456+00:00|Youtube=2020-06-17T02:50:22.415896+00:00|reCAPTCHA=2020-06-30T01:05:55.855499+00:00'
(Pdb) settings.COOKIE_CONSENT_NAME
'cookie_consent'
(Pdb) parse_cookie_str(request.COOKIES.get(settings.COOKIE_CONSENT_NAME))
{'Stripe': '2020-04-01T01:59:44.145456+00:00', 'Youtube': '2020-06-17T02:50:22.415896+00:00', 'reCAPTCHA': '2020-06-30T01:05:55.855499+00:00'}

I could not get pdb to run when AttributeError: 'str' object has no attribute 'COOKIES' occurs. I narrowed it down to get_decline_cookie_groups_cookie_string (cookie_consent_tags.py), get_cookie_dict_from_request (util.py), and {% get_decline_cookie_groups_cookie_string request cookie_groups %} (test_page.html) because getting rid of all of these things gets rid of the problem. (keep the try and except but delete the test_page.html tag)

Writing print(request, type(request)) in get_cookie_dict_from_request produces <class 'str'>.

I added more information to the issue. Hopefully it helps.

9mido avatar Jul 28 '20 18:07 9mido

I agree with @bmihelac that this approach isn't desired, as it hides the actual underlying problem. I will close this PR to clean up the repository, but leave the issue open so we can use that to further investigate what is happening.

sergei-maertens avatar Sep 04 '22 21:09 sergei-maertens