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

Added logging for errors from Turnstile #2533

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
9 changes: 9 additions & 0 deletions concordia/turnstile/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import inspect
import json
from logging import getLogger
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import ProxyHandler, Request, build_opener
Expand All @@ -13,6 +14,8 @@

from ..turnstile.widgets import TurnstileWidget

logger = getLogger(__name__)


class TurnstileField(forms.Field):
widget = TurnstileWidget
Expand Down Expand Up @@ -64,13 +67,19 @@ def validate(self, value):
try:
response = opener.open(request, timeout=settings.TURNSTILE_TIMEOUT)
except HTTPError as exc:
logger.exception("HTTPError received from Turnstile: ", exc, exc_info=exc)
raise forms.ValidationError(
self.error_messages["error_turnstile"], code="error_turnstile"
) from exc

response_data = json.loads(response.read().decode("utf-8"))

if not response_data.get("success"):
logger.exception(
"Failure received from Turnstile. Error codes: %s. Messages: %s",
response_data.get("error-codes"),
response_data.get("messages"),
)
raise forms.ValidationError(
self.error_messages["invalid_turnstile"], code="invalid_turnstile"
)